xref: /onnv-gate/usr/src/uts/common/io/mouse8042.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
23*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
24*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * PS/2 type Mouse Module - Streams
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <sys/param.h>
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <sys/kmem.h>
40*0Sstevel@tonic-gate #include <sys/signal.h>
41*0Sstevel@tonic-gate #include <sys/errno.h>
42*0Sstevel@tonic-gate #include <sys/file.h>
43*0Sstevel@tonic-gate #include <sys/termio.h>
44*0Sstevel@tonic-gate #include <sys/stream.h>
45*0Sstevel@tonic-gate #include <sys/stropts.h>
46*0Sstevel@tonic-gate #include <sys/strtty.h>
47*0Sstevel@tonic-gate #include <sys/debug.h>
48*0Sstevel@tonic-gate #include <sys/ddi.h>
49*0Sstevel@tonic-gate #include <sys/stat.h>
50*0Sstevel@tonic-gate #include <sys/cmn_err.h>
51*0Sstevel@tonic-gate #include <sys/sunddi.h>
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #include <sys/promif.h>
54*0Sstevel@tonic-gate #include <sys/cred.h>
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #include <sys/i8042.h>
57*0Sstevel@tonic-gate #include <sys/note.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #define	DRIVER_NAME(dip)	ddi_driver_name(dip)
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #ifdef	DEBUG
62*0Sstevel@tonic-gate #define	MOUSE8042_DEBUG
63*0Sstevel@tonic-gate #endif
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #define	MOUSE8042_INTERNAL_OPEN(minor)	(((minor) & 0x1) == 1)
66*0Sstevel@tonic-gate #define	MOUSE8042_MINOR_TO_INSTANCE(minor)	((minor) / 2)
67*0Sstevel@tonic-gate #define	MOUSE8042_INTERNAL_MINOR(minor)		((minor) + 1)
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate extern int ddi_create_internal_pathname(dev_info_t *, char *, int, minor_t);
70*0Sstevel@tonic-gate extern void consconfig_link(major_t major, minor_t minor);
71*0Sstevel@tonic-gate extern int consconfig_unlink(major_t major, minor_t minor);
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate  *
76*0Sstevel@tonic-gate  * Local Static Data
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate  * We only support one instance.  Yes, it's theoretically possible to
82*0Sstevel@tonic-gate  * plug in more than one, but it's not worth the implementation cost.
83*0Sstevel@tonic-gate  *
84*0Sstevel@tonic-gate  * The introduction of USB keyboards might make it worth reassessing
85*0Sstevel@tonic-gate  * this decision, as they might free up the keyboard port for a second
86*0Sstevel@tonic-gate  * PS/2 style mouse.
87*0Sstevel@tonic-gate  */
88*0Sstevel@tonic-gate static dev_info_t *mouse8042_dip;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate struct mouse_state {
91*0Sstevel@tonic-gate 	queue_t	*ms_rqp;
92*0Sstevel@tonic-gate 	queue_t	*ms_wqp;
93*0Sstevel@tonic-gate 	ddi_iblock_cookie_t	ms_iblock_cookie;
94*0Sstevel@tonic-gate 	ddi_acc_handle_t	ms_handle;
95*0Sstevel@tonic-gate 	uint8_t			*ms_addr;
96*0Sstevel@tonic-gate 	kmutex_t		ms_mutex;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	minor_t			ms_minor;
99*0Sstevel@tonic-gate 	boolean_t		ms_opened;
100*0Sstevel@tonic-gate };
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate #if	defined(MOUSE8042_DEBUG)
103*0Sstevel@tonic-gate int mouse8042_debug = 0;
104*0Sstevel@tonic-gate int mouse8042_debug_minimal = 0;
105*0Sstevel@tonic-gate #endif
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate static uint_t mouse8042_intr(caddr_t arg);
108*0Sstevel@tonic-gate static int mouse8042_open(queue_t *q, dev_t *devp, int flag, int sflag,
109*0Sstevel@tonic-gate 		cred_t *cred_p);
110*0Sstevel@tonic-gate static int mouse8042_close(queue_t *q, int flag, cred_t *cred_p);
111*0Sstevel@tonic-gate static int mouse8042_wput(queue_t *q, mblk_t *mp);
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate static int mouse8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
114*0Sstevel@tonic-gate 		void *arg, void **result);
115*0Sstevel@tonic-gate static int mouse8042_attach(dev_info_t *dev, ddi_attach_cmd_t cmd);
116*0Sstevel@tonic-gate static int mouse8042_detach(dev_info_t *dev, ddi_detach_cmd_t cmd);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate /*
120*0Sstevel@tonic-gate  * Streams module info.
121*0Sstevel@tonic-gate  */
122*0Sstevel@tonic-gate #define	MODULE_NAME	"mouse8042"
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate static struct module_info	mouse8042_minfo = {
125*0Sstevel@tonic-gate 	23,		/* Module ID number */
126*0Sstevel@tonic-gate 	MODULE_NAME,
127*0Sstevel@tonic-gate 	0, INFPSZ,	/* minimum & maximum packet sizes */
128*0Sstevel@tonic-gate 	256, 128	/* hi and low water marks */
129*0Sstevel@tonic-gate };
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate static struct qinit mouse8042_rinit = {
132*0Sstevel@tonic-gate 	NULL,		/* put */
133*0Sstevel@tonic-gate 	NULL,		/* service */
134*0Sstevel@tonic-gate 	mouse8042_open,
135*0Sstevel@tonic-gate 	mouse8042_close,
136*0Sstevel@tonic-gate 	NULL,		/* admin */
137*0Sstevel@tonic-gate 	&mouse8042_minfo,
138*0Sstevel@tonic-gate 	NULL		/* statistics */
139*0Sstevel@tonic-gate };
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static struct qinit mouse8042_winit = {
142*0Sstevel@tonic-gate 	mouse8042_wput,	/* put */
143*0Sstevel@tonic-gate 	NULL,		/* service */
144*0Sstevel@tonic-gate 	NULL,		/* open */
145*0Sstevel@tonic-gate 	NULL,		/* close */
146*0Sstevel@tonic-gate 	NULL,		/* admin */
147*0Sstevel@tonic-gate 	&mouse8042_minfo,
148*0Sstevel@tonic-gate 	NULL		/* statistics */
149*0Sstevel@tonic-gate };
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate static struct streamtab mouse8042_strinfo = {
152*0Sstevel@tonic-gate 	&mouse8042_rinit,
153*0Sstevel@tonic-gate 	&mouse8042_winit,
154*0Sstevel@tonic-gate 	NULL,		/* muxrinit */
155*0Sstevel@tonic-gate 	NULL,		/* muxwinit */
156*0Sstevel@tonic-gate };
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate  * Local Function Declarations
160*0Sstevel@tonic-gate  */
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate static struct cb_ops	mouse8042_cb_ops = {
163*0Sstevel@tonic-gate 	nodev,			/* open */
164*0Sstevel@tonic-gate 	nodev,			/* close */
165*0Sstevel@tonic-gate 	nodev,			/* strategy */
166*0Sstevel@tonic-gate 	nodev,			/* print */
167*0Sstevel@tonic-gate 	nodev,			/* dump */
168*0Sstevel@tonic-gate 	nodev,			/* read */
169*0Sstevel@tonic-gate 	nodev,			/* write */
170*0Sstevel@tonic-gate 	nodev,			/* ioctl */
171*0Sstevel@tonic-gate 	nodev,			/* devmap */
172*0Sstevel@tonic-gate 	nodev,			/* mmap */
173*0Sstevel@tonic-gate 	nodev,			/* segmap */
174*0Sstevel@tonic-gate 	nochpoll,		/* poll */
175*0Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
176*0Sstevel@tonic-gate 	&mouse8042_strinfo,	/* streamtab  */
177*0Sstevel@tonic-gate 	D_MP | D_NEW
178*0Sstevel@tonic-gate };
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate static struct dev_ops	mouse8042_ops = {
182*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
183*0Sstevel@tonic-gate 	0,			/* refcnt  */
184*0Sstevel@tonic-gate 	mouse8042_getinfo,	/* getinfo */
185*0Sstevel@tonic-gate 	nulldev,		/* identify */
186*0Sstevel@tonic-gate 	nulldev,		/* probe */
187*0Sstevel@tonic-gate 	mouse8042_attach,	/* attach */
188*0Sstevel@tonic-gate 	mouse8042_detach,	/* detach */
189*0Sstevel@tonic-gate 	nodev,			/* reset */
190*0Sstevel@tonic-gate 	&mouse8042_cb_ops,	/* driver operations */
191*0Sstevel@tonic-gate 	(struct bus_ops *)0	/* bus operations */
192*0Sstevel@tonic-gate };
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate /*
195*0Sstevel@tonic-gate  * This is the loadable module wrapper.
196*0Sstevel@tonic-gate  */
197*0Sstevel@tonic-gate #include <sys/modctl.h>
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate extern struct mod_ops mod_driverops;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate  * Module linkage information for the kernel.
203*0Sstevel@tonic-gate  */
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate static struct modldrv modldrv = {
206*0Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a driver */
207*0Sstevel@tonic-gate 	"PS/2 Mouse %I%, %E%",
208*0Sstevel@tonic-gate 	&mouse8042_ops,	/* driver ops */
209*0Sstevel@tonic-gate };
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
212*0Sstevel@tonic-gate 	MODREV_1,
213*0Sstevel@tonic-gate 	(void *)&modldrv,
214*0Sstevel@tonic-gate 	NULL
215*0Sstevel@tonic-gate };
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /*
218*0Sstevel@tonic-gate  * This is the driver initialization routine.
219*0Sstevel@tonic-gate  */
220*0Sstevel@tonic-gate int
221*0Sstevel@tonic-gate _init()
222*0Sstevel@tonic-gate {
223*0Sstevel@tonic-gate 	int	rv;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	rv = mod_install(&modlinkage);
226*0Sstevel@tonic-gate 	return (rv);
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate int
231*0Sstevel@tonic-gate _fini(void)
232*0Sstevel@tonic-gate {
233*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate int
238*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
239*0Sstevel@tonic-gate {
240*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate static int
244*0Sstevel@tonic-gate mouse8042_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate 	struct mouse_state *state;
247*0Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
248*0Sstevel@tonic-gate 	static ddi_device_acc_attr_t attr = {
249*0Sstevel@tonic-gate 		DDI_DEVICE_ATTR_V0,
250*0Sstevel@tonic-gate 		DDI_NEVERSWAP_ACC,
251*0Sstevel@tonic-gate 		DDI_STRICTORDER_ACC,
252*0Sstevel@tonic-gate 	};
253*0Sstevel@tonic-gate 	int rc;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
257*0Sstevel@tonic-gate 	if (mouse8042_debug) {
258*0Sstevel@tonic-gate 		cmn_err(CE_CONT, MODULE_NAME "_attach entry\n");
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate #endif
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
263*0Sstevel@tonic-gate 		return (DDI_FAILURE);
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	if (mouse8042_dip != NULL)
266*0Sstevel@tonic-gate 		return (DDI_FAILURE);
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	/* allocate and initialize state structure */
269*0Sstevel@tonic-gate 	state = kmem_zalloc(sizeof (struct mouse_state), KM_SLEEP);
270*0Sstevel@tonic-gate 	state->ms_opened = B_FALSE;
271*0Sstevel@tonic-gate 	ddi_set_driver_private(dip, state);
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	/*
274*0Sstevel@tonic-gate 	 * In order to support virtual keyboard/mouse, we should distinguish
275*0Sstevel@tonic-gate 	 * between internal virtual open and external physical open.
276*0Sstevel@tonic-gate 	 *
277*0Sstevel@tonic-gate 	 * When the physical devices are opened by application, they will
278*0Sstevel@tonic-gate 	 * be unlinked from the virtual device and their data stream will
279*0Sstevel@tonic-gate 	 * not be sent to the virtual device. When the opened physical
280*0Sstevel@tonic-gate 	 * devices are closed, they will be relinked to the virtual devices.
281*0Sstevel@tonic-gate 	 *
282*0Sstevel@tonic-gate 	 * All these automatic switch between virtual and physical are
283*0Sstevel@tonic-gate 	 * transparent.
284*0Sstevel@tonic-gate 	 *
285*0Sstevel@tonic-gate 	 * So we change minor node numbering scheme to be:
286*0Sstevel@tonic-gate 	 * 	external node minor num == instance * 2
287*0Sstevel@tonic-gate 	 *	internal node minor num == instance * 2 + 1
288*0Sstevel@tonic-gate 	 */
289*0Sstevel@tonic-gate 	rc = ddi_create_minor_node(dip, "l", S_IFCHR, instance * 2,
290*0Sstevel@tonic-gate 	    DDI_NT_MOUSE, NULL);
291*0Sstevel@tonic-gate 	if (rc != DDI_SUCCESS) {
292*0Sstevel@tonic-gate #if	defined(MOUSE8042_DEBUG)
293*0Sstevel@tonic-gate 		cmn_err(CE_CONT,
294*0Sstevel@tonic-gate 		    MODULE_NAME "_attach: ddi_create_minor_node failed\n");
295*0Sstevel@tonic-gate #endif
296*0Sstevel@tonic-gate 		goto fail_1;
297*0Sstevel@tonic-gate 	}
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	if (ddi_create_internal_pathname(dip, "internal_mouse", S_IFCHR,
300*0Sstevel@tonic-gate 		    instance * 2 + 1) != DDI_SUCCESS) {
301*0Sstevel@tonic-gate 		goto fail_2;
302*0Sstevel@tonic-gate 	}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	rc = ddi_regs_map_setup(dip, 0, (caddr_t *)&state->ms_addr,
305*0Sstevel@tonic-gate 		(offset_t)0, (offset_t)0, &attr, &state->ms_handle);
306*0Sstevel@tonic-gate 	if (rc != DDI_SUCCESS) {
307*0Sstevel@tonic-gate #if	defined(MOUSE8042_DEBUG)
308*0Sstevel@tonic-gate 		cmn_err(CE_WARN, MODULE_NAME "_attach:  can't map registers");
309*0Sstevel@tonic-gate #endif
310*0Sstevel@tonic-gate 		goto fail_2;
311*0Sstevel@tonic-gate 	}
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	rc = ddi_get_iblock_cookie(dip, 0, &state->ms_iblock_cookie);
314*0Sstevel@tonic-gate 	if (rc != DDI_SUCCESS) {
315*0Sstevel@tonic-gate #if	defined(MOUSE8042_DEBUG)
316*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
317*0Sstevel@tonic-gate 		    MODULE_NAME "_attach:  Can't get iblock cookie");
318*0Sstevel@tonic-gate #endif
319*0Sstevel@tonic-gate 		goto fail_3;
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	mutex_init(&state->ms_mutex, NULL, MUTEX_DRIVER,
323*0Sstevel@tonic-gate 	    state->ms_iblock_cookie);
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	rc = ddi_add_intr(dip, 0,
326*0Sstevel@tonic-gate 		(ddi_iblock_cookie_t *)NULL, (ddi_idevice_cookie_t *)NULL,
327*0Sstevel@tonic-gate 		mouse8042_intr, (caddr_t)state);
328*0Sstevel@tonic-gate 	if (rc != DDI_SUCCESS) {
329*0Sstevel@tonic-gate #if	defined(MOUSE8042_DEBUG)
330*0Sstevel@tonic-gate 		cmn_err(CE_WARN, MODULE_NAME "_attach: cannot add interrupt");
331*0Sstevel@tonic-gate #endif
332*0Sstevel@tonic-gate 		goto fail_3;
333*0Sstevel@tonic-gate 	}
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	mouse8042_dip = dip;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	/* Now that we're attached, announce our presence to the world. */
338*0Sstevel@tonic-gate 	ddi_report_dev(dip);
339*0Sstevel@tonic-gate #if	defined(MOUSE8042_DEBUG)
340*0Sstevel@tonic-gate 	cmn_err(CE_CONT, "?%s #%d: version %s\n",
341*0Sstevel@tonic-gate 	    DRIVER_NAME(dip), ddi_get_instance(dip), "%I% (%E%)");
342*0Sstevel@tonic-gate #endif
343*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate fail_3:
346*0Sstevel@tonic-gate 	ddi_regs_map_free(&state->ms_handle);
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate fail_2:
349*0Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate fail_1:
352*0Sstevel@tonic-gate 	kmem_free(state, sizeof (struct mouse_state));
353*0Sstevel@tonic-gate 	return (rc);
354*0Sstevel@tonic-gate }
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate /*ARGSUSED*/
357*0Sstevel@tonic-gate static int
358*0Sstevel@tonic-gate mouse8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate 	struct mouse_state *state;
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	state = ddi_get_driver_private(dip);
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	switch (cmd) {
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	case DDI_DETACH:
367*0Sstevel@tonic-gate 		ddi_remove_intr(dip, 0, state->ms_iblock_cookie);
368*0Sstevel@tonic-gate 		mouse8042_dip = NULL;
369*0Sstevel@tonic-gate 		mutex_destroy(&state->ms_mutex);
370*0Sstevel@tonic-gate 		ddi_prop_remove_all(dip);
371*0Sstevel@tonic-gate 		ddi_regs_map_free(&state->ms_handle);
372*0Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
373*0Sstevel@tonic-gate 		kmem_free(state, sizeof (struct mouse_state));
374*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	default:
377*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
378*0Sstevel@tonic-gate 		if (mouse8042_debug) {
379*0Sstevel@tonic-gate 			cmn_err(CE_CONT,
380*0Sstevel@tonic-gate 			    "mouse8042_detach: cmd = %d unknown\n", cmd);
381*0Sstevel@tonic-gate 		}
382*0Sstevel@tonic-gate #endif
383*0Sstevel@tonic-gate 		return (DDI_FAILURE);
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate }
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate /* ARGSUSED */
389*0Sstevel@tonic-gate static int
390*0Sstevel@tonic-gate mouse8042_getinfo(
391*0Sstevel@tonic-gate     dev_info_t *dip,
392*0Sstevel@tonic-gate     ddi_info_cmd_t infocmd,
393*0Sstevel@tonic-gate     void *arg,
394*0Sstevel@tonic-gate     void **result)
395*0Sstevel@tonic-gate {
396*0Sstevel@tonic-gate 	dev_t dev = (dev_t)arg;
397*0Sstevel@tonic-gate 	minor_t	minor = getminor(dev);
398*0Sstevel@tonic-gate 	int	instance = MOUSE8042_MINOR_TO_INSTANCE(minor);
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
401*0Sstevel@tonic-gate 	if (mouse8042_debug)
402*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "mouse8042_getinfo: call\n");
403*0Sstevel@tonic-gate #endif
404*0Sstevel@tonic-gate 	switch (infocmd) {
405*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
406*0Sstevel@tonic-gate 		if (mouse8042_dip == NULL)
407*0Sstevel@tonic-gate 			return (DDI_FAILURE);
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 		*result = (void *)mouse8042_dip;
410*0Sstevel@tonic-gate 		break;
411*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
412*0Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
413*0Sstevel@tonic-gate 		break;
414*0Sstevel@tonic-gate 	default:
415*0Sstevel@tonic-gate 		return (DDI_FAILURE);
416*0Sstevel@tonic-gate 	}
417*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
418*0Sstevel@tonic-gate }
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate /*ARGSUSED*/
421*0Sstevel@tonic-gate static int
422*0Sstevel@tonic-gate mouse8042_open(
423*0Sstevel@tonic-gate 	queue_t	*q,
424*0Sstevel@tonic-gate 	dev_t	*devp,
425*0Sstevel@tonic-gate 	int	flag,
426*0Sstevel@tonic-gate 	int	sflag,
427*0Sstevel@tonic-gate 	cred_t	*cred_p)
428*0Sstevel@tonic-gate {
429*0Sstevel@tonic-gate 	struct mouse_state *state;
430*0Sstevel@tonic-gate 	minor_t	minor = getminor(*devp);
431*0Sstevel@tonic-gate 	int rval;
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	if (mouse8042_dip == NULL)
434*0Sstevel@tonic-gate 		return (ENXIO);
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	state = ddi_get_driver_private(mouse8042_dip);
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
439*0Sstevel@tonic-gate 	if (mouse8042_debug)
440*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "mouse8042_open:entered\n");
441*0Sstevel@tonic-gate #endif
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	mutex_enter(&state->ms_mutex);
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	if (state->ms_opened) {
446*0Sstevel@tonic-gate 		/*
447*0Sstevel@tonic-gate 		 * Exit if the same minor node is already open
448*0Sstevel@tonic-gate 		 */
449*0Sstevel@tonic-gate 		if (state->ms_minor == minor) {
450*0Sstevel@tonic-gate 			mutex_exit(&state->ms_mutex);
451*0Sstevel@tonic-gate 			return (0);
452*0Sstevel@tonic-gate 		}
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 		/*
455*0Sstevel@tonic-gate 		 * Check whether it is switch between physical and virtual
456*0Sstevel@tonic-gate 		 *
457*0Sstevel@tonic-gate 		 * Opening from virtual while the device is being physically
458*0Sstevel@tonic-gate 		 * opened by an application should not happen. So we ASSERT
459*0Sstevel@tonic-gate 		 * this in DEBUG version, and return error in the non-DEBUG
460*0Sstevel@tonic-gate 		 * case.
461*0Sstevel@tonic-gate 		 */
462*0Sstevel@tonic-gate 		ASSERT(!MOUSE8042_INTERNAL_OPEN(minor));
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 		if (MOUSE8042_INTERNAL_OPEN(minor)) {
465*0Sstevel@tonic-gate 			mutex_exit(&state->ms_mutex);
466*0Sstevel@tonic-gate 			return (EINVAL);
467*0Sstevel@tonic-gate 		}
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 		/*
470*0Sstevel@tonic-gate 		 * Opening the physical one while it is being underneath
471*0Sstevel@tonic-gate 		 * the virtual one.
472*0Sstevel@tonic-gate 		 *
473*0Sstevel@tonic-gate 		 * consconfig_unlink is called to unlink this device from
474*0Sstevel@tonic-gate 		 * the virtual one, thus the old stream serving for this
475*0Sstevel@tonic-gate 		 * device under the virtual one is closed, and then the
476*0Sstevel@tonic-gate 		 * lower driver's close routine (here is mouse8042_close)
477*0Sstevel@tonic-gate 		 * is also called to accomplish the whole stream close.
478*0Sstevel@tonic-gate 		 * Here we have to drop the lock because mouse8042_close
479*0Sstevel@tonic-gate 		 * also needs the lock.
480*0Sstevel@tonic-gate 		 *
481*0Sstevel@tonic-gate 		 * For mouse, the old stream is:
482*0Sstevel@tonic-gate 		 *	consms->["pushmod"->]"mouse_vp driver"
483*0Sstevel@tonic-gate 		 *
484*0Sstevel@tonic-gate 		 * After the consconfig_unlink returns, the old stream is closed
485*0Sstevel@tonic-gate 		 * and we grab the lock again to reopen this device as normal.
486*0Sstevel@tonic-gate 		 */
487*0Sstevel@tonic-gate 		mutex_exit(&state->ms_mutex);
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 		/*
490*0Sstevel@tonic-gate 		 * If unlink fails, fail the physical open.
491*0Sstevel@tonic-gate 		 */
492*0Sstevel@tonic-gate 		if ((rval = consconfig_unlink(ddi_driver_major(mouse8042_dip),
493*0Sstevel@tonic-gate 		    MOUSE8042_INTERNAL_MINOR(minor))) != 0) {
494*0Sstevel@tonic-gate 			return (rval);
495*0Sstevel@tonic-gate 		}
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 		mutex_enter(&state->ms_mutex);
498*0Sstevel@tonic-gate 	}
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	q->q_ptr = (caddr_t)state;
502*0Sstevel@tonic-gate 	WR(q)->q_ptr = (caddr_t)state;
503*0Sstevel@tonic-gate 	state->ms_rqp = q;
504*0Sstevel@tonic-gate 	state->ms_wqp = WR(q);
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate 	qprocson(q);
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 	state->ms_minor = minor;
509*0Sstevel@tonic-gate 	state->ms_opened = B_TRUE;
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	mutex_exit(&state->ms_mutex);
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	return (0);
514*0Sstevel@tonic-gate }
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate /*ARGSUSED*/
518*0Sstevel@tonic-gate static int
519*0Sstevel@tonic-gate mouse8042_close(queue_t *q, int flag, cred_t *cred_p)
520*0Sstevel@tonic-gate {
521*0Sstevel@tonic-gate 	struct mouse_state *state;
522*0Sstevel@tonic-gate 	minor_t	minor;
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate 	state = (struct mouse_state *)q->q_ptr;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
527*0Sstevel@tonic-gate 	if (mouse8042_debug)
528*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "mouse8042_close:entered\n");
529*0Sstevel@tonic-gate #endif
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	mutex_enter(&state->ms_mutex);
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	qprocsoff(q);
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	q->q_ptr = NULL;
536*0Sstevel@tonic-gate 	WR(q)->q_ptr = NULL;
537*0Sstevel@tonic-gate 	state->ms_rqp = NULL;
538*0Sstevel@tonic-gate 	state->ms_wqp = NULL;
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	state->ms_opened = B_FALSE;
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	minor = state->ms_minor;
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	mutex_exit(&state->ms_mutex);
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	if (!MOUSE8042_INTERNAL_OPEN(minor)) {
547*0Sstevel@tonic-gate 		/*
548*0Sstevel@tonic-gate 		 * Closing physical PS/2 mouse
549*0Sstevel@tonic-gate 		 *
550*0Sstevel@tonic-gate 		 * Link it back to virtual mouse, and
551*0Sstevel@tonic-gate 		 * mouse8042_open will be called as a result
552*0Sstevel@tonic-gate 		 * of the consconfig_link call.
553*0Sstevel@tonic-gate 		 *
554*0Sstevel@tonic-gate 		 * If linking back fails, this specific mouse
555*0Sstevel@tonic-gate 		 * will not be available underneath the virtual
556*0Sstevel@tonic-gate 		 * mouse, and can only be accessed via physical
557*0Sstevel@tonic-gate 		 * open.
558*0Sstevel@tonic-gate 		 */
559*0Sstevel@tonic-gate 		consconfig_link(ddi_driver_major(mouse8042_dip),
560*0Sstevel@tonic-gate 		    MOUSE8042_INTERNAL_MINOR(minor));
561*0Sstevel@tonic-gate 	}
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 	return (0);
564*0Sstevel@tonic-gate }
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate static void
567*0Sstevel@tonic-gate mouse8042_iocnack(
568*0Sstevel@tonic-gate     queue_t *qp,
569*0Sstevel@tonic-gate     mblk_t *mp,
570*0Sstevel@tonic-gate     struct iocblk *iocp,
571*0Sstevel@tonic-gate     int error,
572*0Sstevel@tonic-gate     int rval)
573*0Sstevel@tonic-gate {
574*0Sstevel@tonic-gate 	mp->b_datap->db_type = M_IOCNAK;
575*0Sstevel@tonic-gate 	iocp->ioc_rval = rval;
576*0Sstevel@tonic-gate 	iocp->ioc_error = error;
577*0Sstevel@tonic-gate 	qreply(qp, mp);
578*0Sstevel@tonic-gate }
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate static int
581*0Sstevel@tonic-gate mouse8042_wput(queue_t *q, mblk_t *mp)
582*0Sstevel@tonic-gate {
583*0Sstevel@tonic-gate 	struct iocblk *iocbp;
584*0Sstevel@tonic-gate 	mblk_t *bp;
585*0Sstevel@tonic-gate 	mblk_t *next;
586*0Sstevel@tonic-gate 	struct mouse_state *state;
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 	state = (struct mouse_state *)q->q_ptr;
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
591*0Sstevel@tonic-gate 	if (mouse8042_debug)
592*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "mouse8042_wput:entered\n");
593*0Sstevel@tonic-gate #endif
594*0Sstevel@tonic-gate 	iocbp = (struct iocblk *)mp->b_rptr;
595*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
596*0Sstevel@tonic-gate 	case M_FLUSH:
597*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
598*0Sstevel@tonic-gate 		if (mouse8042_debug)
599*0Sstevel@tonic-gate 			cmn_err(CE_CONT, "mouse8042_wput:M_FLUSH\n");
600*0Sstevel@tonic-gate #endif
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW)
603*0Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
604*0Sstevel@tonic-gate 		qreply(q, mp);
605*0Sstevel@tonic-gate 		break;
606*0Sstevel@tonic-gate 	case M_IOCTL:
607*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
608*0Sstevel@tonic-gate 		if (mouse8042_debug)
609*0Sstevel@tonic-gate 			cmn_err(CE_CONT, "mouse8042_wput:M_IOCTL\n");
610*0Sstevel@tonic-gate #endif
611*0Sstevel@tonic-gate 		mouse8042_iocnack(q, mp, iocbp, EINVAL, 0);
612*0Sstevel@tonic-gate 		break;
613*0Sstevel@tonic-gate 	case M_IOCDATA:
614*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
615*0Sstevel@tonic-gate 		if (mouse8042_debug)
616*0Sstevel@tonic-gate 			cmn_err(CE_CONT, "mouse8042_wput:M_IOCDATA\n");
617*0Sstevel@tonic-gate #endif
618*0Sstevel@tonic-gate 		mouse8042_iocnack(q, mp, iocbp, EINVAL, 0);
619*0Sstevel@tonic-gate 		break;
620*0Sstevel@tonic-gate 	case M_DATA:
621*0Sstevel@tonic-gate 		bp = mp;
622*0Sstevel@tonic-gate 		do {
623*0Sstevel@tonic-gate 			while (bp->b_rptr < bp->b_wptr) {
624*0Sstevel@tonic-gate #if	defined(MOUSE8042_DEBUG)
625*0Sstevel@tonic-gate 				if (mouse8042_debug) {
626*0Sstevel@tonic-gate 					cmn_err(CE_CONT,
627*0Sstevel@tonic-gate 					    "mouse8042:  send %2x\n",
628*0Sstevel@tonic-gate 					    *bp->b_rptr);
629*0Sstevel@tonic-gate 				}
630*0Sstevel@tonic-gate 				if (mouse8042_debug_minimal) {
631*0Sstevel@tonic-gate 					cmn_err(CE_CONT, ">a:%2x ",
632*0Sstevel@tonic-gate 					    *bp->b_rptr);
633*0Sstevel@tonic-gate 				}
634*0Sstevel@tonic-gate #endif
635*0Sstevel@tonic-gate 				ddi_put8(state->ms_handle,
636*0Sstevel@tonic-gate 					state->ms_addr + I8042_INT_OUTPUT_DATA,
637*0Sstevel@tonic-gate 					*bp->b_rptr++);
638*0Sstevel@tonic-gate 			}
639*0Sstevel@tonic-gate 			next = bp->b_cont;
640*0Sstevel@tonic-gate 			freeb(bp);
641*0Sstevel@tonic-gate 		} while ((bp = next) != NULL);
642*0Sstevel@tonic-gate 		break;
643*0Sstevel@tonic-gate 	default:
644*0Sstevel@tonic-gate 		freemsg(mp);
645*0Sstevel@tonic-gate 		break;
646*0Sstevel@tonic-gate 	}
647*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
648*0Sstevel@tonic-gate 	if (mouse8042_debug)
649*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "mouse8042_wput:leaving\n");
650*0Sstevel@tonic-gate #endif
651*0Sstevel@tonic-gate 	return (0);	/* ignored */
652*0Sstevel@tonic-gate }
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate static uint_t
655*0Sstevel@tonic-gate mouse8042_intr(caddr_t arg)
656*0Sstevel@tonic-gate {
657*0Sstevel@tonic-gate 	unsigned char    mdata;
658*0Sstevel@tonic-gate 	mblk_t *mp;
659*0Sstevel@tonic-gate 	struct mouse_state *state = (struct mouse_state *)arg;
660*0Sstevel@tonic-gate 	int rc;
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 	mutex_enter(&state->ms_mutex);
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate #if	defined(MOUSE8042_DEBUG)
665*0Sstevel@tonic-gate 	if (mouse8042_debug)
666*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "mouse8042_intr()\n");
667*0Sstevel@tonic-gate #endif
668*0Sstevel@tonic-gate 	rc = DDI_INTR_UNCLAIMED;
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate 	for (;;) {
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate 		if (ddi_get8(state->ms_handle,
673*0Sstevel@tonic-gate 			    state->ms_addr + I8042_INT_INPUT_AVAIL) == 0) {
674*0Sstevel@tonic-gate 			break;
675*0Sstevel@tonic-gate 		}
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 		mdata = ddi_get8(state->ms_handle,
678*0Sstevel@tonic-gate 				state->ms_addr + I8042_INT_INPUT_DATA);
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate #if	defined(MOUSE8042_DEBUG)
681*0Sstevel@tonic-gate 		if (mouse8042_debug)
682*0Sstevel@tonic-gate 			cmn_err(CE_CONT, "mouse8042_intr:  got %2x\n", mdata);
683*0Sstevel@tonic-gate 		if (mouse8042_debug_minimal)
684*0Sstevel@tonic-gate 			cmn_err(CE_CONT, "<A:%2x ", mdata);
685*0Sstevel@tonic-gate #endif
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 		rc = DDI_INTR_CLAIMED;
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 		if (state->ms_rqp != NULL && (mp = allocb(1, BPRI_MED))) {
690*0Sstevel@tonic-gate 			*mp->b_wptr++ = mdata;
691*0Sstevel@tonic-gate 			putnext(state->ms_rqp, mp);
692*0Sstevel@tonic-gate 		}
693*0Sstevel@tonic-gate 	}
694*0Sstevel@tonic-gate #ifdef MOUSE8042_DEBUG
695*0Sstevel@tonic-gate 	if (mouse8042_debug)
696*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "mouse8042_intr() ok\n");
697*0Sstevel@tonic-gate #endif
698*0Sstevel@tonic-gate 	mutex_exit(&state->ms_mutex);
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 	return (rc);
701*0Sstevel@tonic-gate }
702