xref: /onnv-gate/usr/src/uts/common/io/consms.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 /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Console mouse driver for Sun.
31*0Sstevel@tonic-gate  * The console "zs" port is linked under us, with the "ms" module pushed
32*0Sstevel@tonic-gate  * on top of it.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * This device merely provides a way to have "/dev/mouse" automatically
35*0Sstevel@tonic-gate  * have the "ms" module present. Due to problems with the way the "specfs"
36*0Sstevel@tonic-gate  * file system works, you can't use an indirect device (a "stat" on
37*0Sstevel@tonic-gate  * "/dev/mouse" won't get the right snode, so you won't get the right time
38*0Sstevel@tonic-gate  * of last access), and due to problems with the kernel window system code,
39*0Sstevel@tonic-gate  * you can't use a "cons"-like driver ("/dev/mouse" won't be a streams device,
40*0Sstevel@tonic-gate  * even though operations on it get turned into operations on the real stream).
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * This module supports multiple mice connected to the system at the same time.
43*0Sstevel@tonic-gate  * All the mice are linked under consms, and act as a mouse with replicated
44*0Sstevel@tonic-gate  * clicks. Only USB and PS/2 mouse are supported to be virtual mouse now.
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #include <sys/types.h>
48*0Sstevel@tonic-gate #include <sys/param.h>
49*0Sstevel@tonic-gate #include <sys/stropts.h>
50*0Sstevel@tonic-gate #include <sys/stream.h>
51*0Sstevel@tonic-gate #include <sys/strsun.h>
52*0Sstevel@tonic-gate #include <sys/conf.h>
53*0Sstevel@tonic-gate #include <sys/stat.h>
54*0Sstevel@tonic-gate #include <sys/errno.h>
55*0Sstevel@tonic-gate #include <sys/modctl.h>
56*0Sstevel@tonic-gate #include <sys/consdev.h>
57*0Sstevel@tonic-gate #include <sys/ddi.h>
58*0Sstevel@tonic-gate #include <sys/sunddi.h>
59*0Sstevel@tonic-gate #include <sys/kstat.h>
60*0Sstevel@tonic-gate #include <sys/vuid_wheel.h>
61*0Sstevel@tonic-gate #include <sys/msio.h>
62*0Sstevel@tonic-gate #include <sys/consms.h>
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static void consms_plink(queue_t *, mblk_t *);
65*0Sstevel@tonic-gate static int consms_punlink(queue_t *, mblk_t *);
66*0Sstevel@tonic-gate static void
67*0Sstevel@tonic-gate consms_lqs_ack_complete(consms_lq_t *, mblk_t *);
68*0Sstevel@tonic-gate static void consms_add_lq(consms_lq_t *);
69*0Sstevel@tonic-gate static void consms_check_caps(void);
70*0Sstevel@tonic-gate static mblk_t *consms_new_firm_event(int, int);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate static void consms_mux_max_wheel_report(mblk_t *);
73*0Sstevel@tonic-gate static void consms_mux_cache_states(mblk_t *);
74*0Sstevel@tonic-gate static void consms_mux_link_msg(consms_msg_t *);
75*0Sstevel@tonic-gate static consms_msg_t *consms_mux_unlink_msg(uint_t);
76*0Sstevel@tonic-gate static consms_msg_t *consms_mux_find_msg(uint_t);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate static void consms_mux_iocdata(consms_msg_t *, mblk_t *);
79*0Sstevel@tonic-gate static void consms_mux_disp_iocdata(consms_response_t *, mblk_t *);
80*0Sstevel@tonic-gate static int consms_mux_disp_ioctl(queue_t *, mblk_t *);
81*0Sstevel@tonic-gate static void consms_mux_copyreq(queue_t *, consms_msg_t *, mblk_t *);
82*0Sstevel@tonic-gate static void consms_mux_ack(consms_msg_t *, mblk_t *);
83*0Sstevel@tonic-gate static void consms_mux_disp_data(mblk_t *);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate static int	consmsopen();
87*0Sstevel@tonic-gate static int	consmsclose();
88*0Sstevel@tonic-gate static void	consmsuwput();
89*0Sstevel@tonic-gate static void	consmslrput();
90*0Sstevel@tonic-gate static void	consmslwserv();
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate static struct module_info consmsm_info = {
93*0Sstevel@tonic-gate 	0,
94*0Sstevel@tonic-gate 	"consms",
95*0Sstevel@tonic-gate 	0,
96*0Sstevel@tonic-gate 	1024,
97*0Sstevel@tonic-gate 	2048,
98*0Sstevel@tonic-gate 	128
99*0Sstevel@tonic-gate };
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate static struct qinit consmsurinit = {
102*0Sstevel@tonic-gate 	putq,
103*0Sstevel@tonic-gate 	(int (*)())NULL,
104*0Sstevel@tonic-gate 	consmsopen,
105*0Sstevel@tonic-gate 	consmsclose,
106*0Sstevel@tonic-gate 	(int (*)())NULL,
107*0Sstevel@tonic-gate 	&consmsm_info,
108*0Sstevel@tonic-gate 	NULL
109*0Sstevel@tonic-gate };
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate static struct qinit consmsuwinit = {
112*0Sstevel@tonic-gate 	(int (*)())consmsuwput,
113*0Sstevel@tonic-gate 	(int (*)())NULL,
114*0Sstevel@tonic-gate 	consmsopen,
115*0Sstevel@tonic-gate 	consmsclose,
116*0Sstevel@tonic-gate 	(int (*)())NULL,
117*0Sstevel@tonic-gate 	&consmsm_info,
118*0Sstevel@tonic-gate 	NULL
119*0Sstevel@tonic-gate };
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate static struct qinit consmslrinit = {
122*0Sstevel@tonic-gate 	(int (*)())consmslrput,
123*0Sstevel@tonic-gate 	(int (*)())NULL,
124*0Sstevel@tonic-gate 	(int (*)())NULL,
125*0Sstevel@tonic-gate 	(int (*)())NULL,
126*0Sstevel@tonic-gate 	(int (*)())NULL,
127*0Sstevel@tonic-gate 	&consmsm_info,
128*0Sstevel@tonic-gate 	NULL
129*0Sstevel@tonic-gate };
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate static struct qinit consmslwinit = {
132*0Sstevel@tonic-gate 	putq,
133*0Sstevel@tonic-gate 	(int (*)())consmslwserv,
134*0Sstevel@tonic-gate 	(int (*)())NULL,
135*0Sstevel@tonic-gate 	(int (*)())NULL,
136*0Sstevel@tonic-gate 	(int (*)())NULL,
137*0Sstevel@tonic-gate 	&consmsm_info,
138*0Sstevel@tonic-gate 	NULL
139*0Sstevel@tonic-gate };
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static struct streamtab consms_str_info = {
142*0Sstevel@tonic-gate 	&consmsurinit,
143*0Sstevel@tonic-gate 	&consmsuwinit,
144*0Sstevel@tonic-gate 	&consmslrinit,
145*0Sstevel@tonic-gate 	&consmslwinit,
146*0Sstevel@tonic-gate };
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate static void consmsioctl(queue_t *q, mblk_t *mp);
149*0Sstevel@tonic-gate static int consms_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
150*0Sstevel@tonic-gate 		void **result);
151*0Sstevel@tonic-gate static int consms_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
152*0Sstevel@tonic-gate static int consms_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
153*0Sstevel@tonic-gate static int consms_kstat_update(kstat_t *, int);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate  * Module global data are protected by the per-module inner perimeter.
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate static queue_t		*upperqueue;	/* regular mouse queue above us */
159*0Sstevel@tonic-gate static dev_info_t	*consms_dip;	/* private copy of devinfo pointer */
160*0Sstevel@tonic-gate static long	consms_idle_stamp;	/* seconds tstamp of latest mouse op */
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate static consms_msg_t	*consms_mux_msg; /* ioctl messages being processed */
163*0Sstevel@tonic-gate static	kmutex_t	consms_msg_lock; /* protect ioctl messages list */
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate static consms_state_t	consms_state;	/* the global virtual mouse state */
166*0Sstevel@tonic-gate static	kmutex_t	consmslock;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate /*
170*0Sstevel@tonic-gate  * Normally, kstats of type KSTAT_TYPE_NAMED have multiple elements.  In
171*0Sstevel@tonic-gate  * this case we use this type for a single element because the ioctl code
172*0Sstevel@tonic-gate  * for it knows how to handle mixed kernel/user data models.  Also, it
173*0Sstevel@tonic-gate  * will be easier to add new statistics later.
174*0Sstevel@tonic-gate  */
175*0Sstevel@tonic-gate static struct {
176*0Sstevel@tonic-gate 	kstat_named_t idle_sec;		/* seconds since last user op */
177*0Sstevel@tonic-gate } consms_kstat = {
178*0Sstevel@tonic-gate 	{ "idle_sec", KSTAT_DATA_LONG, }
179*0Sstevel@tonic-gate };
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate static 	struct cb_ops cb_consms_ops = {
183*0Sstevel@tonic-gate 	nulldev,		/* cb_open */
184*0Sstevel@tonic-gate 	nulldev,		/* cb_close */
185*0Sstevel@tonic-gate 	nodev,			/* cb_strategy */
186*0Sstevel@tonic-gate 	nodev,			/* cb_print */
187*0Sstevel@tonic-gate 	nodev,			/* cb_dump */
188*0Sstevel@tonic-gate 	nodev,			/* cb_read */
189*0Sstevel@tonic-gate 	nodev,			/* cb_write */
190*0Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
191*0Sstevel@tonic-gate 	nodev,			/* cb_devmap */
192*0Sstevel@tonic-gate 	nodev,			/* cb_mmap */
193*0Sstevel@tonic-gate 	nodev,			/* cb_segmap */
194*0Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
195*0Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
196*0Sstevel@tonic-gate 	&consms_str_info,	/* cb_stream */
197*0Sstevel@tonic-gate 	D_MP | D_MTPERMOD	/* cb_flag */
198*0Sstevel@tonic-gate };
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate static struct dev_ops consms_ops = {
201*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
202*0Sstevel@tonic-gate 	0,			/* devo_refcnt */
203*0Sstevel@tonic-gate 	consms_info,		/* devo_getinfo */
204*0Sstevel@tonic-gate 	nulldev,		/* devo_identify */
205*0Sstevel@tonic-gate 	nulldev,		/* devo_probe */
206*0Sstevel@tonic-gate 	consms_attach,		/* devo_attach */
207*0Sstevel@tonic-gate 	consms_detach,		/* devo_detach */
208*0Sstevel@tonic-gate 	nodev,			/* devo_reset */
209*0Sstevel@tonic-gate 	&(cb_consms_ops),	/* devo_cb_ops */
210*0Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
211*0Sstevel@tonic-gate 	NULL			/* devo_power */
212*0Sstevel@tonic-gate };
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate /*
216*0Sstevel@tonic-gate  * Module linkage information for the kernel.
217*0Sstevel@tonic-gate  */
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate static struct modldrv modldrv = {
220*0Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
221*0Sstevel@tonic-gate 	"Mouse Driver for Sun 'consms' %I%",
222*0Sstevel@tonic-gate 	&consms_ops,	/* driver ops */
223*0Sstevel@tonic-gate };
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
226*0Sstevel@tonic-gate 	MODREV_1,
227*0Sstevel@tonic-gate 	(void *)&modldrv,
228*0Sstevel@tonic-gate 	NULL
229*0Sstevel@tonic-gate };
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate int
232*0Sstevel@tonic-gate _init(void)
233*0Sstevel@tonic-gate {
234*0Sstevel@tonic-gate 	int	error;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	mutex_init(&consmslock, NULL, MUTEX_DRIVER, NULL);
237*0Sstevel@tonic-gate 	mutex_init(&consms_msg_lock, NULL, MUTEX_DRIVER, NULL);
238*0Sstevel@tonic-gate 	error = mod_install(&modlinkage);
239*0Sstevel@tonic-gate 	if (error != 0) {
240*0Sstevel@tonic-gate 		mutex_destroy(&consmslock);
241*0Sstevel@tonic-gate 		mutex_destroy(&consms_msg_lock);
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 	return (error);
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate int
247*0Sstevel@tonic-gate _fini(void)
248*0Sstevel@tonic-gate {
249*0Sstevel@tonic-gate 	int	error;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
252*0Sstevel@tonic-gate 	if (error != 0)
253*0Sstevel@tonic-gate 		return (error);
254*0Sstevel@tonic-gate 	mutex_destroy(&consmslock);
255*0Sstevel@tonic-gate 	mutex_destroy(&consms_msg_lock);
256*0Sstevel@tonic-gate 	return (0);
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate int
260*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
261*0Sstevel@tonic-gate {
262*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate static int
266*0Sstevel@tonic-gate consms_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
267*0Sstevel@tonic-gate {
268*0Sstevel@tonic-gate 	kstat_t	*ksp;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	switch (cmd) {
271*0Sstevel@tonic-gate 	case DDI_ATTACH:
272*0Sstevel@tonic-gate 		break;
273*0Sstevel@tonic-gate 	default:
274*0Sstevel@tonic-gate 		return (DDI_FAILURE);
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "mouse", S_IFCHR,
278*0Sstevel@tonic-gate 		0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
279*0Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
280*0Sstevel@tonic-gate 		return (-1);
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 	consms_dip = devi;
283*0Sstevel@tonic-gate 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, devi, DDI_NO_AUTODETACH, 1);
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	ksp = kstat_create("consms", 0, "activity", "misc", KSTAT_TYPE_NAMED,
286*0Sstevel@tonic-gate 	    sizeof (consms_kstat) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
287*0Sstevel@tonic-gate 	if (ksp) {
288*0Sstevel@tonic-gate 		ksp->ks_data = (void *)&consms_kstat;
289*0Sstevel@tonic-gate 		ksp->ks_update = consms_kstat_update;
290*0Sstevel@tonic-gate 		kstat_install(ksp);
291*0Sstevel@tonic-gate 		consms_idle_stamp = gethrestime_sec();	/* initial value */
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	consms_state.consms_lqs = NULL;
295*0Sstevel@tonic-gate 	consms_state.consms_num_lqs = 0;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	/* default consms state values */
298*0Sstevel@tonic-gate 	consms_state.consms_vuid_format = VUID_FIRM_EVENT;
299*0Sstevel@tonic-gate 	consms_state.consms_num_buttons = 0;
300*0Sstevel@tonic-gate 	consms_state.consms_num_wheels = 0;
301*0Sstevel@tonic-gate 	consms_state.consms_wheel_state_bf |= VUID_WHEEL_STATE_ENABLED;
302*0Sstevel@tonic-gate 	consms_state.consms_ms_parms.jitter_thresh =
303*0Sstevel@tonic-gate 	    CONSMS_PARMS_DEFAULT_JITTER;
304*0Sstevel@tonic-gate 	consms_state.consms_ms_parms.speed_limit =
305*0Sstevel@tonic-gate 	    CONSMS_PARMS_DEFAULT_SPEED_LIMIT;
306*0Sstevel@tonic-gate 	consms_state.consms_ms_parms.speed_law =
307*0Sstevel@tonic-gate 	    CONSMS_PARMS_DEFAULT_SPEED_LAW;
308*0Sstevel@tonic-gate 	consms_state.consms_ms_sr.height = CONSMS_SR_DEFAULT_HEIGHT;
309*0Sstevel@tonic-gate 	consms_state.consms_ms_sr.width = CONSMS_SR_DEFAULT_WIDTH;
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
312*0Sstevel@tonic-gate }
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate /*ARGSUSED*/
315*0Sstevel@tonic-gate static int
316*0Sstevel@tonic-gate consms_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
317*0Sstevel@tonic-gate {
318*0Sstevel@tonic-gate 	switch (cmd) {
319*0Sstevel@tonic-gate 	case DDI_DETACH:
320*0Sstevel@tonic-gate 	default:
321*0Sstevel@tonic-gate 		return (DDI_FAILURE);
322*0Sstevel@tonic-gate 	}
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate /*ARGSUSED*/
326*0Sstevel@tonic-gate static int
327*0Sstevel@tonic-gate consms_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
328*0Sstevel@tonic-gate 	void **result)
329*0Sstevel@tonic-gate {
330*0Sstevel@tonic-gate 	register int error;
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	switch (infocmd) {
333*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
334*0Sstevel@tonic-gate 		if (consms_dip == NULL) {
335*0Sstevel@tonic-gate 			error = DDI_FAILURE;
336*0Sstevel@tonic-gate 		} else {
337*0Sstevel@tonic-gate 			*result = (void *) consms_dip;
338*0Sstevel@tonic-gate 			error = DDI_SUCCESS;
339*0Sstevel@tonic-gate 		}
340*0Sstevel@tonic-gate 		break;
341*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
342*0Sstevel@tonic-gate 		*result = (void *)0;
343*0Sstevel@tonic-gate 		error = DDI_SUCCESS;
344*0Sstevel@tonic-gate 		break;
345*0Sstevel@tonic-gate 	default:
346*0Sstevel@tonic-gate 		error = DDI_FAILURE;
347*0Sstevel@tonic-gate 	}
348*0Sstevel@tonic-gate 	return (error);
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate /*ARGSUSED*/
353*0Sstevel@tonic-gate static int
354*0Sstevel@tonic-gate consmsopen(q, devp, flag, sflag, crp)
355*0Sstevel@tonic-gate 	queue_t *q;
356*0Sstevel@tonic-gate 	dev_t	*devp;
357*0Sstevel@tonic-gate 	int	flag, sflag;
358*0Sstevel@tonic-gate 	cred_t	*crp;
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate 	upperqueue = q;
361*0Sstevel@tonic-gate 	qprocson(q);
362*0Sstevel@tonic-gate 	return (0);
363*0Sstevel@tonic-gate }
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate /*ARGSUSED*/
366*0Sstevel@tonic-gate static int
367*0Sstevel@tonic-gate consmsclose(q, flag, crp)
368*0Sstevel@tonic-gate 	queue_t *q;
369*0Sstevel@tonic-gate 	int	flag;
370*0Sstevel@tonic-gate 	cred_t	*crp;
371*0Sstevel@tonic-gate {
372*0Sstevel@tonic-gate 	qprocsoff(q);
373*0Sstevel@tonic-gate 	upperqueue = NULL;
374*0Sstevel@tonic-gate 	return (0);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate /*
378*0Sstevel@tonic-gate  * Put procedure for upper write queue.
379*0Sstevel@tonic-gate  */
380*0Sstevel@tonic-gate static void
381*0Sstevel@tonic-gate consmsuwput(q, mp)
382*0Sstevel@tonic-gate 	register queue_t *q;
383*0Sstevel@tonic-gate 	register mblk_t *mp;
384*0Sstevel@tonic-gate {
385*0Sstevel@tonic-gate 	struct iocblk		*iocbp = (struct iocblk *)mp->b_rptr;
386*0Sstevel@tonic-gate 	consms_msg_t		*msg;
387*0Sstevel@tonic-gate 	int			error = 0;
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	case M_IOCTL:
392*0Sstevel@tonic-gate 		consmsioctl(q, mp);
393*0Sstevel@tonic-gate 		break;
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	case M_FLUSH:
396*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW)
397*0Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
398*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR)
399*0Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
400*0Sstevel@tonic-gate 		if (consms_state.consms_num_lqs > 0) {
401*0Sstevel@tonic-gate 			consms_mux_disp_data(mp);
402*0Sstevel@tonic-gate 		} else {
403*0Sstevel@tonic-gate 			/*
404*0Sstevel@tonic-gate 			 * No lower queue; just reflect this back upstream.
405*0Sstevel@tonic-gate 			 */
406*0Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
407*0Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHR)
408*0Sstevel@tonic-gate 				qreply(q, mp);
409*0Sstevel@tonic-gate 			else
410*0Sstevel@tonic-gate 				freemsg(mp);
411*0Sstevel@tonic-gate 		}
412*0Sstevel@tonic-gate 		break;
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	case M_DATA:
415*0Sstevel@tonic-gate 		if (consms_state.consms_num_lqs > 0) {
416*0Sstevel@tonic-gate 			consms_mux_disp_data(mp);
417*0Sstevel@tonic-gate 		} else {
418*0Sstevel@tonic-gate 			error = EINVAL;
419*0Sstevel@tonic-gate 		}
420*0Sstevel@tonic-gate 		break;
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	case M_IOCDATA:
423*0Sstevel@tonic-gate 		if ((msg = consms_mux_find_msg(iocbp->ioc_id)) != NULL) {
424*0Sstevel@tonic-gate 			consms_mux_iocdata(msg, mp);
425*0Sstevel@tonic-gate 		} else {
426*0Sstevel@tonic-gate 			error = EINVAL;
427*0Sstevel@tonic-gate 		}
428*0Sstevel@tonic-gate 		break;
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	default:
431*0Sstevel@tonic-gate 		error = EINVAL;
432*0Sstevel@tonic-gate 		break;
433*0Sstevel@tonic-gate 	}
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	if (error) {
436*0Sstevel@tonic-gate 		/*
437*0Sstevel@tonic-gate 		 * Pass an error message up.
438*0Sstevel@tonic-gate 		 */
439*0Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
440*0Sstevel@tonic-gate 		if (mp->b_cont) {
441*0Sstevel@tonic-gate 			freemsg(mp->b_cont);
442*0Sstevel@tonic-gate 			mp->b_cont = NULL;
443*0Sstevel@tonic-gate 		}
444*0Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
445*0Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
446*0Sstevel@tonic-gate 		*mp->b_rptr = (char)error;
447*0Sstevel@tonic-gate 		qreply(q, mp);
448*0Sstevel@tonic-gate 	}
449*0Sstevel@tonic-gate }
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate static void
452*0Sstevel@tonic-gate consmsioctl(q, mp)
453*0Sstevel@tonic-gate 	register queue_t *q;
454*0Sstevel@tonic-gate 	register mblk_t *mp;
455*0Sstevel@tonic-gate {
456*0Sstevel@tonic-gate 	register struct iocblk *iocp;
457*0Sstevel@tonic-gate 	int		error;
458*0Sstevel@tonic-gate 	mblk_t		*datap;
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	case I_LINK:
465*0Sstevel@tonic-gate 	case I_PLINK:
466*0Sstevel@tonic-gate 		mutex_enter(&consmslock);
467*0Sstevel@tonic-gate 		consms_plink(q, mp);
468*0Sstevel@tonic-gate 		mutex_exit(&consmslock);
469*0Sstevel@tonic-gate 		return;
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	case I_UNLINK:
472*0Sstevel@tonic-gate 	case I_PUNLINK:
473*0Sstevel@tonic-gate 		mutex_enter(&consmslock);
474*0Sstevel@tonic-gate 		if ((error = consms_punlink(q, mp)) != 0) {
475*0Sstevel@tonic-gate 			mutex_exit(&consmslock);
476*0Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
477*0Sstevel@tonic-gate 			return;
478*0Sstevel@tonic-gate 		}
479*0Sstevel@tonic-gate 		mutex_exit(&consmslock);
480*0Sstevel@tonic-gate 		iocp->ioc_count = 0;
481*0Sstevel@tonic-gate 		break;
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	case MSIOBUTTONS:	/* query the number of buttons */
484*0Sstevel@tonic-gate 		if ((consms_state.consms_num_lqs <= 0) ||
485*0Sstevel@tonic-gate 		    ((datap = allocb(sizeof (int), BPRI_HI)) == NULL)) {
486*0Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
487*0Sstevel@tonic-gate 			return;
488*0Sstevel@tonic-gate 		}
489*0Sstevel@tonic-gate 		*(int *)datap->b_wptr = consms_state.consms_num_buttons;
490*0Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
491*0Sstevel@tonic-gate 		if (mp->b_cont) {
492*0Sstevel@tonic-gate 			freemsg(mp->b_cont);
493*0Sstevel@tonic-gate 		}
494*0Sstevel@tonic-gate 		mp->b_cont = datap;
495*0Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
496*0Sstevel@tonic-gate 		break;
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	default:
499*0Sstevel@tonic-gate 		/*
500*0Sstevel@tonic-gate 		 * Pass this through, if there's something to pass it
501*0Sstevel@tonic-gate 		 * through to; otherwise, reject it.
502*0Sstevel@tonic-gate 		 */
503*0Sstevel@tonic-gate 		if (consms_state.consms_num_lqs <= 0) {
504*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
505*0Sstevel@tonic-gate 			return;
506*0Sstevel@tonic-gate 		}
507*0Sstevel@tonic-gate 		if ((error = consms_mux_disp_ioctl(q, mp)) != 0)
508*0Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 		return;
511*0Sstevel@tonic-gate 	}
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	/*
514*0Sstevel@tonic-gate 	 * Common exit path for calls that return a positive
515*0Sstevel@tonic-gate 	 * acknowledgment with a return value of 0.
516*0Sstevel@tonic-gate 	 */
517*0Sstevel@tonic-gate 	miocack(q, mp, iocp->ioc_count, 0);
518*0Sstevel@tonic-gate }
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate /*
521*0Sstevel@tonic-gate  * Service procedure for lower write queue.
522*0Sstevel@tonic-gate  * Puts things on the queue below us, if it lets us.
523*0Sstevel@tonic-gate  */
524*0Sstevel@tonic-gate static void
525*0Sstevel@tonic-gate consmslwserv(q)
526*0Sstevel@tonic-gate 	register queue_t *q;
527*0Sstevel@tonic-gate {
528*0Sstevel@tonic-gate 	register mblk_t *mp;
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 	while (canput(q->q_next) && (mp = getq(q)) != NULL)
531*0Sstevel@tonic-gate 		putnext(q, mp);
532*0Sstevel@tonic-gate }
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate /*
535*0Sstevel@tonic-gate  * Put procedure for lower read queue.
536*0Sstevel@tonic-gate  */
537*0Sstevel@tonic-gate static void
538*0Sstevel@tonic-gate consmslrput(q, mp)
539*0Sstevel@tonic-gate 	register queue_t *q;
540*0Sstevel@tonic-gate 	register mblk_t *mp;
541*0Sstevel@tonic-gate {
542*0Sstevel@tonic-gate 	struct iocblk		*iocbp = (struct iocblk *)mp->b_rptr;
543*0Sstevel@tonic-gate 	struct copyreq		*copyreq = (struct copyreq *)mp->b_rptr;
544*0Sstevel@tonic-gate 	consms_msg_t		*msg;
545*0Sstevel@tonic-gate 	consms_lq_t		*lq = (consms_lq_t *)q->q_ptr;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	ASSERT(lq != NULL);
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
550*0Sstevel@tonic-gate 	case M_FLUSH:
551*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW)
552*0Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
553*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR)
554*0Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
555*0Sstevel@tonic-gate 		if (upperqueue != NULL)
556*0Sstevel@tonic-gate 			putnext(upperqueue, mp);	/* pass it through */
557*0Sstevel@tonic-gate 		else {
558*0Sstevel@tonic-gate 			/*
559*0Sstevel@tonic-gate 			 * No upper queue; just reflect this back downstream.
560*0Sstevel@tonic-gate 			 */
561*0Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;
562*0Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW)
563*0Sstevel@tonic-gate 				qreply(q, mp);
564*0Sstevel@tonic-gate 			else
565*0Sstevel@tonic-gate 				freemsg(mp);
566*0Sstevel@tonic-gate 		}
567*0Sstevel@tonic-gate 		break;
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	case M_DATA:
570*0Sstevel@tonic-gate 		if (upperqueue != NULL)
571*0Sstevel@tonic-gate 			putnext(upperqueue, mp);
572*0Sstevel@tonic-gate 		else
573*0Sstevel@tonic-gate 			freemsg(mp);
574*0Sstevel@tonic-gate 		consms_idle_stamp = gethrestime_sec();
575*0Sstevel@tonic-gate 		break;
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	case M_IOCACK:
578*0Sstevel@tonic-gate 	case M_IOCNAK:
579*0Sstevel@tonic-gate 		/*
580*0Sstevel@tonic-gate 		 * First, check to see if this device
581*0Sstevel@tonic-gate 		 * is still being initialized.
582*0Sstevel@tonic-gate 		 */
583*0Sstevel@tonic-gate 		if (lq->lq_ioc_reply_func != NULL) {
584*0Sstevel@tonic-gate 			mutex_enter(&consmslock);
585*0Sstevel@tonic-gate 			lq->lq_ioc_reply_func(lq, mp);
586*0Sstevel@tonic-gate 			mutex_exit(&consmslock);
587*0Sstevel@tonic-gate 			freemsg(mp);
588*0Sstevel@tonic-gate 			break;
589*0Sstevel@tonic-gate 		}
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate 		/*
592*0Sstevel@tonic-gate 		 * This is normal ioctl ack for upper layer.
593*0Sstevel@tonic-gate 		 */
594*0Sstevel@tonic-gate 		if ((msg = consms_mux_find_msg(iocbp->ioc_id)) != NULL) {
595*0Sstevel@tonic-gate 			consms_mux_ack(msg, mp);
596*0Sstevel@tonic-gate 		} else {
597*0Sstevel@tonic-gate 			freemsg(mp);
598*0Sstevel@tonic-gate 		}
599*0Sstevel@tonic-gate 		consms_idle_stamp = gethrestime_sec();
600*0Sstevel@tonic-gate 		break;
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	case M_COPYIN:
603*0Sstevel@tonic-gate 	case M_COPYOUT:
604*0Sstevel@tonic-gate 		if ((msg = consms_mux_find_msg(copyreq->cq_id)) != NULL) {
605*0Sstevel@tonic-gate 			consms_mux_copyreq(q, msg, mp);
606*0Sstevel@tonic-gate 		} else
607*0Sstevel@tonic-gate 			freemsg(mp);
608*0Sstevel@tonic-gate 		consms_idle_stamp = gethrestime_sec();
609*0Sstevel@tonic-gate 		break;
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 	case M_ERROR:
612*0Sstevel@tonic-gate 	case M_HANGUP:
613*0Sstevel@tonic-gate 	default:
614*0Sstevel@tonic-gate 		freemsg(mp);	/* anything useful here? */
615*0Sstevel@tonic-gate 		break;
616*0Sstevel@tonic-gate 	}
617*0Sstevel@tonic-gate }
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate /* ARGSUSED */
620*0Sstevel@tonic-gate static int
621*0Sstevel@tonic-gate consms_kstat_update(kstat_t *ksp, int rw)
622*0Sstevel@tonic-gate {
623*0Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
624*0Sstevel@tonic-gate 		return (EACCES);
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 	consms_kstat.idle_sec.value.l = gethrestime_sec() - consms_idle_stamp;
627*0Sstevel@tonic-gate 	return (0);
628*0Sstevel@tonic-gate }
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate /*ARGSUSED*/
631*0Sstevel@tonic-gate static int
632*0Sstevel@tonic-gate consms_punlink(queue_t *q, mblk_t *mp)
633*0Sstevel@tonic-gate {
634*0Sstevel@tonic-gate 	struct linkblk	*linkp;
635*0Sstevel@tonic-gate 	consms_lq_t	*lq;
636*0Sstevel@tonic-gate 	consms_lq_t	*prev_lq;
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&consmslock));
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 	prev_lq = NULL;
643*0Sstevel@tonic-gate 	for (lq = consms_state.consms_lqs; lq != NULL; lq = lq->lq_next) {
644*0Sstevel@tonic-gate 		if (lq->lq_queue == linkp->l_qbot) {
645*0Sstevel@tonic-gate 			if (prev_lq)
646*0Sstevel@tonic-gate 				prev_lq->lq_next = lq->lq_next;
647*0Sstevel@tonic-gate 			else
648*0Sstevel@tonic-gate 				consms_state.consms_lqs = lq->lq_next;
649*0Sstevel@tonic-gate 			kmem_free(lq, sizeof (*lq));
650*0Sstevel@tonic-gate 			consms_state.consms_num_lqs--;
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 			/*
653*0Sstevel@tonic-gate 			 * Check to see if mouse capabilities
654*0Sstevel@tonic-gate 			 * have changed.
655*0Sstevel@tonic-gate 			 */
656*0Sstevel@tonic-gate 			consms_check_caps();
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 			return (0);
659*0Sstevel@tonic-gate 		}
660*0Sstevel@tonic-gate 		prev_lq = lq;
661*0Sstevel@tonic-gate 	}
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	return (EINVAL);
664*0Sstevel@tonic-gate }
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate /*
667*0Sstevel@tonic-gate  * Link a specific mouse into our mouse list.
668*0Sstevel@tonic-gate  */
669*0Sstevel@tonic-gate static void
670*0Sstevel@tonic-gate consms_plink(queue_t *q, mblk_t *mp)
671*0Sstevel@tonic-gate {
672*0Sstevel@tonic-gate 	struct	linkblk	*linkp;
673*0Sstevel@tonic-gate 	consms_lq_t	*lq;
674*0Sstevel@tonic-gate 	queue_t		*lowq;
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&consmslock));
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
679*0Sstevel@tonic-gate 	lowq = linkp->l_qbot;
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	lq = kmem_zalloc(sizeof (*lq), KM_SLEEP);
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 	lowq->q_ptr = (void *)lq;
684*0Sstevel@tonic-gate 	OTHERQ(lowq)->q_ptr = (void *)lq;
685*0Sstevel@tonic-gate 	lq->lq_queue = lowq;
686*0Sstevel@tonic-gate 	lq->lq_pending_plink = mp;
687*0Sstevel@tonic-gate 	lq->lq_pending_queue = q;
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	/*
690*0Sstevel@tonic-gate 	 * Set the number of buttons to 3 by default
691*0Sstevel@tonic-gate 	 * in case the following MSIOBUTTONS ioctl fails.
692*0Sstevel@tonic-gate 	 */
693*0Sstevel@tonic-gate 	lq->lq_num_buttons = 3;
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 	/*
696*0Sstevel@tonic-gate 	 * Begin to initialize this mouse.
697*0Sstevel@tonic-gate 	 */
698*0Sstevel@tonic-gate 	lq->lq_state = LQS_START;
699*0Sstevel@tonic-gate 	consms_lqs_ack_complete(lq, NULL);
700*0Sstevel@tonic-gate }
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate /*
703*0Sstevel@tonic-gate  * Initialize the newly hotplugged-in mouse,
704*0Sstevel@tonic-gate  * e.g. get the number of buttons, set event
705*0Sstevel@tonic-gate  * format. Then we add it into our list.
706*0Sstevel@tonic-gate  */
707*0Sstevel@tonic-gate static void
708*0Sstevel@tonic-gate consms_lqs_ack_complete(consms_lq_t *lq, mblk_t *mp)
709*0Sstevel@tonic-gate {
710*0Sstevel@tonic-gate 	mblk_t			*req = NULL;
711*0Sstevel@tonic-gate 	boolean_t		skipped = B_FALSE;
712*0Sstevel@tonic-gate 	wheel_state		*ws;
713*0Sstevel@tonic-gate 	Ms_screen_resolution	*sr;
714*0Sstevel@tonic-gate 	Ms_parms		*params;
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&consmslock));
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 	/*
719*0Sstevel@tonic-gate 	 * We try each ioctl even if the previous one fails
720*0Sstevel@tonic-gate 	 * until we reach LQS_DONE, and then add this lq
721*0Sstevel@tonic-gate 	 * into our lq list.
722*0Sstevel@tonic-gate 	 *
723*0Sstevel@tonic-gate 	 * If the message allocation fails, we skip this ioctl,
724*0Sstevel@tonic-gate 	 * set skipped flag to B_TRUE in order to skip the ioctl
725*0Sstevel@tonic-gate 	 * result, then we try next ioctl, go to next state.
726*0Sstevel@tonic-gate 	 */
727*0Sstevel@tonic-gate 	while ((lq->lq_state < LQS_DONE) && (req == NULL)) {
728*0Sstevel@tonic-gate 		switch (lq->lq_state) {
729*0Sstevel@tonic-gate 		case LQS_START:
730*0Sstevel@tonic-gate 			/*
731*0Sstevel@tonic-gate 			 * First, issue MSIOBUTTONS ioctl
732*0Sstevel@tonic-gate 			 * to get the number of buttons.
733*0Sstevel@tonic-gate 			 */
734*0Sstevel@tonic-gate 			req = mkiocb(MSIOBUTTONS);
735*0Sstevel@tonic-gate 			if (req && ((req->b_cont = allocb(sizeof (int),
736*0Sstevel@tonic-gate 			    BPRI_MED)) == NULL)) {
737*0Sstevel@tonic-gate 				freemsg(req);
738*0Sstevel@tonic-gate 				req = NULL;
739*0Sstevel@tonic-gate 			}
740*0Sstevel@tonic-gate 			if (req == NULL)
741*0Sstevel@tonic-gate 				skipped = B_TRUE;
742*0Sstevel@tonic-gate 			lq->lq_state++;
743*0Sstevel@tonic-gate 			break;
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 		case LQS_BUTTON_COUNT_PENDING:
746*0Sstevel@tonic-gate 			if (!skipped && mp && mp->b_cont &&
747*0Sstevel@tonic-gate 			    (mp->b_datap->db_type == M_IOCACK))
748*0Sstevel@tonic-gate 				lq->lq_num_buttons =
749*0Sstevel@tonic-gate 				    *(int *)mp->b_cont->b_rptr;
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 			/*
752*0Sstevel@tonic-gate 			 * Second, issue VUIDGWHEELCOUNT ioctl
753*0Sstevel@tonic-gate 			 * to get the count of wheels.
754*0Sstevel@tonic-gate 			 */
755*0Sstevel@tonic-gate 			req = mkiocb(VUIDGWHEELCOUNT);
756*0Sstevel@tonic-gate 			if (req && ((req->b_cont = allocb(sizeof (int),
757*0Sstevel@tonic-gate 			    BPRI_MED)) == NULL)) {
758*0Sstevel@tonic-gate 				freemsg(req);
759*0Sstevel@tonic-gate 				req = NULL;
760*0Sstevel@tonic-gate 			}
761*0Sstevel@tonic-gate 			if (req == NULL)
762*0Sstevel@tonic-gate 				skipped = B_TRUE;
763*0Sstevel@tonic-gate 			lq->lq_state++;
764*0Sstevel@tonic-gate 			break;
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 		case LQS_WHEEL_COUNT_PENDING:
767*0Sstevel@tonic-gate 			if (!skipped && mp && mp->b_cont &&
768*0Sstevel@tonic-gate 			    (mp->b_datap->db_type == M_IOCACK))
769*0Sstevel@tonic-gate 				lq->lq_num_wheels =
770*0Sstevel@tonic-gate 				    *(int *)mp->b_cont->b_rptr;
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 			/*
773*0Sstevel@tonic-gate 			 * Third, issue VUIDSFORMAT ioctl
774*0Sstevel@tonic-gate 			 * to set the event format.
775*0Sstevel@tonic-gate 			 */
776*0Sstevel@tonic-gate 			req = mkiocb(VUIDSFORMAT);
777*0Sstevel@tonic-gate 			if (req && ((req->b_cont = allocb(sizeof (int),
778*0Sstevel@tonic-gate 			    BPRI_MED)) == NULL)) {
779*0Sstevel@tonic-gate 				freemsg(req);
780*0Sstevel@tonic-gate 				req = NULL;
781*0Sstevel@tonic-gate 			}
782*0Sstevel@tonic-gate 			if (req) {
783*0Sstevel@tonic-gate 				*(int *)req->b_cont->b_wptr =
784*0Sstevel@tonic-gate 				    consms_state.consms_vuid_format;
785*0Sstevel@tonic-gate 				req->b_cont->b_wptr += sizeof (int);
786*0Sstevel@tonic-gate 			}
787*0Sstevel@tonic-gate 			lq->lq_state++;
788*0Sstevel@tonic-gate 			break;
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 		case LQS_SET_VUID_FORMAT_PENDING:
791*0Sstevel@tonic-gate 			/*
792*0Sstevel@tonic-gate 			 * Fourth, issue VUIDSWHEELSTATE ioctl
793*0Sstevel@tonic-gate 			 * to set the wheel state (enable or disable).
794*0Sstevel@tonic-gate 			 */
795*0Sstevel@tonic-gate 			req = mkiocb(VUIDSWHEELSTATE);
796*0Sstevel@tonic-gate 			if (req && ((req->b_cont = allocb(sizeof (wheel_state),
797*0Sstevel@tonic-gate 			    BPRI_MED)) == NULL)) {
798*0Sstevel@tonic-gate 				freemsg(req);
799*0Sstevel@tonic-gate 				req = NULL;
800*0Sstevel@tonic-gate 			}
801*0Sstevel@tonic-gate 			if (req) {
802*0Sstevel@tonic-gate 				ws = (wheel_state *)req->b_cont->b_wptr;
803*0Sstevel@tonic-gate 				ws->vers = VUID_WHEEL_STATE_VERS;
804*0Sstevel@tonic-gate 				ws->id = 0;	/* the first wheel */
805*0Sstevel@tonic-gate 				ws->stateflags =
806*0Sstevel@tonic-gate 				    consms_state.consms_wheel_state_bf & 1;
807*0Sstevel@tonic-gate 				req->b_cont->b_wptr += sizeof (wheel_state);
808*0Sstevel@tonic-gate 			}
809*0Sstevel@tonic-gate 			lq->lq_state++;
810*0Sstevel@tonic-gate 			break;
811*0Sstevel@tonic-gate 
812*0Sstevel@tonic-gate 		case LQS_SET_WHEEL_STATE_PENDING:
813*0Sstevel@tonic-gate 			/*
814*0Sstevel@tonic-gate 			 * Fifth, issue MSIOSRESOLUTION ioctl
815*0Sstevel@tonic-gate 			 * to set the screen resolution for absolute mouse.
816*0Sstevel@tonic-gate 			 */
817*0Sstevel@tonic-gate 			req = mkiocb(MSIOSRESOLUTION);
818*0Sstevel@tonic-gate 			if (req && ((req->b_cont =
819*0Sstevel@tonic-gate 			    allocb(sizeof (Ms_screen_resolution),
820*0Sstevel@tonic-gate 			    BPRI_MED)) == NULL)) {
821*0Sstevel@tonic-gate 				freemsg(req);
822*0Sstevel@tonic-gate 				req = NULL;
823*0Sstevel@tonic-gate 			}
824*0Sstevel@tonic-gate 			if (req) {
825*0Sstevel@tonic-gate 				sr =
826*0Sstevel@tonic-gate 				    (Ms_screen_resolution *)req->b_cont->b_wptr;
827*0Sstevel@tonic-gate 				*sr = consms_state.consms_ms_sr;
828*0Sstevel@tonic-gate 				req->b_cont->b_wptr +=
829*0Sstevel@tonic-gate 				    sizeof (Ms_screen_resolution);
830*0Sstevel@tonic-gate 			}
831*0Sstevel@tonic-gate 			lq->lq_state++;
832*0Sstevel@tonic-gate 			break;
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 		case LQS_SET_RESOLUTION_PENDING:
835*0Sstevel@tonic-gate 			/*
836*0Sstevel@tonic-gate 			 * Sixth, issue MSIOSETPARMS ioctl
837*0Sstevel@tonic-gate 			 * to set the parameters for USB mouse.
838*0Sstevel@tonic-gate 			 */
839*0Sstevel@tonic-gate 			req = mkiocb(MSIOSETPARMS);
840*0Sstevel@tonic-gate 			if (req && ((req->b_cont = allocb(sizeof (Ms_parms),
841*0Sstevel@tonic-gate 			    BPRI_MED)) == NULL)) {
842*0Sstevel@tonic-gate 				freemsg(req);
843*0Sstevel@tonic-gate 				req = NULL;
844*0Sstevel@tonic-gate 			}
845*0Sstevel@tonic-gate 			if (req) {
846*0Sstevel@tonic-gate 				params = (Ms_parms *)req->b_cont->b_wptr;
847*0Sstevel@tonic-gate 				*params = consms_state.consms_ms_parms;
848*0Sstevel@tonic-gate 				req->b_cont->b_wptr += sizeof (Ms_parms);
849*0Sstevel@tonic-gate 			}
850*0Sstevel@tonic-gate 			lq->lq_state++;
851*0Sstevel@tonic-gate 			break;
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate 		case LQS_SET_PARMS_PENDING:
854*0Sstevel@tonic-gate 			/*
855*0Sstevel@tonic-gate 			 * All jobs are done, lq->lq_state is turned into
856*0Sstevel@tonic-gate 			 * LQS_DONE, and this lq is added into our list.
857*0Sstevel@tonic-gate 			 */
858*0Sstevel@tonic-gate 			lq->lq_state++;
859*0Sstevel@tonic-gate 			consms_add_lq(lq);
860*0Sstevel@tonic-gate 			break;
861*0Sstevel@tonic-gate 		}
862*0Sstevel@tonic-gate 	}
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 	if (lq->lq_state < LQS_DONE) {
865*0Sstevel@tonic-gate 		lq->lq_ioc_reply_func = consms_lqs_ack_complete;
866*0Sstevel@tonic-gate 		(void) putq(lq->lq_queue, req);
867*0Sstevel@tonic-gate 	}
868*0Sstevel@tonic-gate }
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate /*
871*0Sstevel@tonic-gate  * Add this specific lq into our list, finally reply
872*0Sstevel@tonic-gate  * the previous pending I_PLINK ioctl. Also check to
873*0Sstevel@tonic-gate  * see if mouse capabilities have changed, and send
874*0Sstevel@tonic-gate  * a dynamical notification event to upper layer if
875*0Sstevel@tonic-gate  * necessary.
876*0Sstevel@tonic-gate  */
877*0Sstevel@tonic-gate static void
878*0Sstevel@tonic-gate consms_add_lq(consms_lq_t *lq)
879*0Sstevel@tonic-gate {
880*0Sstevel@tonic-gate 	struct	iocblk		*iocp;
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&consmslock));
883*0Sstevel@tonic-gate 
884*0Sstevel@tonic-gate 	lq->lq_ioc_reply_func = NULL;
885*0Sstevel@tonic-gate 	iocp = (struct iocblk *)lq->lq_pending_plink->b_rptr;
886*0Sstevel@tonic-gate 	iocp->ioc_error = 0;
887*0Sstevel@tonic-gate 	iocp->ioc_count = 0;
888*0Sstevel@tonic-gate 	iocp->ioc_rval = 0;
889*0Sstevel@tonic-gate 	lq->lq_pending_plink->b_datap->db_type = M_IOCACK;
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 	/* Reply to the I_PLINK ioctl. */
892*0Sstevel@tonic-gate 	qreply(lq->lq_pending_queue, lq->lq_pending_plink);
893*0Sstevel@tonic-gate 
894*0Sstevel@tonic-gate 	lq->lq_pending_plink = NULL;
895*0Sstevel@tonic-gate 	lq->lq_pending_queue = NULL;
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 	/*
898*0Sstevel@tonic-gate 	 * Add this lq into list.
899*0Sstevel@tonic-gate 	 */
900*0Sstevel@tonic-gate 	consms_state.consms_num_lqs++;
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate 	lq->lq_next = consms_state.consms_lqs;
903*0Sstevel@tonic-gate 	consms_state.consms_lqs = lq;
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 	/*
906*0Sstevel@tonic-gate 	 * Check to see if mouse capabilities
907*0Sstevel@tonic-gate 	 * have changed.
908*0Sstevel@tonic-gate 	 */
909*0Sstevel@tonic-gate 	consms_check_caps();
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate }
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate 
914*0Sstevel@tonic-gate static void
915*0Sstevel@tonic-gate consms_check_caps(void)
916*0Sstevel@tonic-gate {
917*0Sstevel@tonic-gate 	consms_lq_t *lq;
918*0Sstevel@tonic-gate 	int	max_buttons = 0;
919*0Sstevel@tonic-gate 	int	max_wheels = 0;
920*0Sstevel@tonic-gate 	mblk_t	*mp;
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate 	/*
923*0Sstevel@tonic-gate 	 * Check to see if the number of buttons
924*0Sstevel@tonic-gate 	 * and the number of wheels have changed.
925*0Sstevel@tonic-gate 	 */
926*0Sstevel@tonic-gate 	for (lq = consms_state.consms_lqs; lq != NULL; lq = lq->lq_next) {
927*0Sstevel@tonic-gate 		max_buttons = CONSMS_MAX(max_buttons, lq->lq_num_buttons);
928*0Sstevel@tonic-gate 		max_wheels = CONSMS_MAX(max_wheels, lq->lq_num_wheels);
929*0Sstevel@tonic-gate 	}
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate 	if (max_buttons != consms_state.consms_num_buttons) {
932*0Sstevel@tonic-gate 		/*
933*0Sstevel@tonic-gate 		 * Since the number of buttons have changed,
934*0Sstevel@tonic-gate 		 * send a MOUSE_CAP_CHANGE_NUM_BUT dynamical
935*0Sstevel@tonic-gate 		 * notification event to upper layer.
936*0Sstevel@tonic-gate 		 */
937*0Sstevel@tonic-gate 		consms_state.consms_num_buttons = max_buttons;
938*0Sstevel@tonic-gate 		if (upperqueue != NULL) {
939*0Sstevel@tonic-gate 			if ((mp = consms_new_firm_event(
940*0Sstevel@tonic-gate 			    MOUSE_CAP_CHANGE_NUM_BUT,
941*0Sstevel@tonic-gate 			    consms_state.consms_num_buttons)) != NULL) {
942*0Sstevel@tonic-gate 				putnext(upperqueue, mp);
943*0Sstevel@tonic-gate 			}
944*0Sstevel@tonic-gate 		}
945*0Sstevel@tonic-gate 	}
946*0Sstevel@tonic-gate 
947*0Sstevel@tonic-gate 	if (max_wheels != consms_state.consms_num_wheels) {
948*0Sstevel@tonic-gate 		/*
949*0Sstevel@tonic-gate 		 * Since the number of wheels have changed,
950*0Sstevel@tonic-gate 		 * send a MOUSE_CAP_CHANGE_NUM_WHEEL dynamical
951*0Sstevel@tonic-gate 		 * notification event to upper layer.
952*0Sstevel@tonic-gate 		 */
953*0Sstevel@tonic-gate 		consms_state.consms_num_wheels = max_wheels;
954*0Sstevel@tonic-gate 		if (upperqueue != NULL) {
955*0Sstevel@tonic-gate 			if ((mp = consms_new_firm_event(
956*0Sstevel@tonic-gate 			    MOUSE_CAP_CHANGE_NUM_WHEEL,
957*0Sstevel@tonic-gate 			    consms_state.consms_num_wheels)) != NULL) {
958*0Sstevel@tonic-gate 				putnext(upperqueue, mp);
959*0Sstevel@tonic-gate 			}
960*0Sstevel@tonic-gate 		}
961*0Sstevel@tonic-gate 	}
962*0Sstevel@tonic-gate }
963*0Sstevel@tonic-gate 
964*0Sstevel@tonic-gate /*
965*0Sstevel@tonic-gate  * Allocate a dynamical notification event.
966*0Sstevel@tonic-gate  */
967*0Sstevel@tonic-gate static mblk_t *
968*0Sstevel@tonic-gate consms_new_firm_event(int id, int value)
969*0Sstevel@tonic-gate {
970*0Sstevel@tonic-gate 	Firm_event *fep;
971*0Sstevel@tonic-gate 	mblk_t	*tmp;
972*0Sstevel@tonic-gate 
973*0Sstevel@tonic-gate 	if ((tmp = allocb(sizeof (Firm_event), BPRI_HI)) != NULL) {
974*0Sstevel@tonic-gate 		fep = (Firm_event *)tmp->b_wptr;
975*0Sstevel@tonic-gate 		fep->id = id;
976*0Sstevel@tonic-gate 		fep->pair_type = FE_PAIR_NONE;
977*0Sstevel@tonic-gate 		fep->pair = NULL;
978*0Sstevel@tonic-gate 		fep->value = value;
979*0Sstevel@tonic-gate 		tmp->b_wptr += sizeof (Firm_event);
980*0Sstevel@tonic-gate 	}
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 	return (tmp);
983*0Sstevel@tonic-gate }
984*0Sstevel@tonic-gate 
985*0Sstevel@tonic-gate /*
986*0Sstevel@tonic-gate  * Start of dispatching interfaces as a multiplexor
987*0Sstevel@tonic-gate  */
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate /*
990*0Sstevel@tonic-gate  * There is a global msg list (consms_mux_msg),
991*0Sstevel@tonic-gate  * which is used to link all ioctl messages from
992*0Sstevel@tonic-gate  * upper layer, which are currently being processed.
993*0Sstevel@tonic-gate  *
994*0Sstevel@tonic-gate  * consms_mux_link_msg links a msg into the list,
995*0Sstevel@tonic-gate  * consms_mux_unlink_msg unlinks a msg from the list,
996*0Sstevel@tonic-gate  * consms_mux_find_msg finds a msg from the list
997*0Sstevel@tonic-gate  * according to its unique id.
998*0Sstevel@tonic-gate  *
999*0Sstevel@tonic-gate  * The id of each msg is taken from stream's mp,
1000*0Sstevel@tonic-gate  * so the id is supposed to be unique.
1001*0Sstevel@tonic-gate  */
1002*0Sstevel@tonic-gate static void
1003*0Sstevel@tonic-gate consms_mux_link_msg(consms_msg_t *msg)
1004*0Sstevel@tonic-gate {
1005*0Sstevel@tonic-gate 	mutex_enter(&consms_msg_lock);
1006*0Sstevel@tonic-gate 	msg->msg_next = consms_mux_msg;
1007*0Sstevel@tonic-gate 	consms_mux_msg = msg;
1008*0Sstevel@tonic-gate 	mutex_exit(&consms_msg_lock);
1009*0Sstevel@tonic-gate }
1010*0Sstevel@tonic-gate 
1011*0Sstevel@tonic-gate static consms_msg_t *
1012*0Sstevel@tonic-gate consms_mux_unlink_msg(uint_t msg_id)
1013*0Sstevel@tonic-gate {
1014*0Sstevel@tonic-gate 	consms_msg_t	*msg;
1015*0Sstevel@tonic-gate 	consms_msg_t	*prev_msg;
1016*0Sstevel@tonic-gate 
1017*0Sstevel@tonic-gate 	mutex_enter(&consms_msg_lock);
1018*0Sstevel@tonic-gate 	prev_msg = NULL;
1019*0Sstevel@tonic-gate 	for (msg = consms_mux_msg; msg != NULL;
1020*0Sstevel@tonic-gate 	    prev_msg = msg, msg = msg->msg_next) {
1021*0Sstevel@tonic-gate 		if (msg->msg_id == msg_id)
1022*0Sstevel@tonic-gate 			break;
1023*0Sstevel@tonic-gate 	}
1024*0Sstevel@tonic-gate 
1025*0Sstevel@tonic-gate 	if (msg != NULL) {
1026*0Sstevel@tonic-gate 		if (prev_msg != NULL) {
1027*0Sstevel@tonic-gate 			prev_msg->msg_next = msg->msg_next;
1028*0Sstevel@tonic-gate 		} else {
1029*0Sstevel@tonic-gate 			consms_mux_msg = consms_mux_msg->msg_next;
1030*0Sstevel@tonic-gate 		}
1031*0Sstevel@tonic-gate 		msg->msg_next = NULL;
1032*0Sstevel@tonic-gate 	}
1033*0Sstevel@tonic-gate 	mutex_exit(&consms_msg_lock);
1034*0Sstevel@tonic-gate 
1035*0Sstevel@tonic-gate 	return (msg);
1036*0Sstevel@tonic-gate }
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate static consms_msg_t *
1039*0Sstevel@tonic-gate consms_mux_find_msg(uint_t msg_id)
1040*0Sstevel@tonic-gate {
1041*0Sstevel@tonic-gate 	consms_msg_t	*msg;
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate 	mutex_enter(&consms_msg_lock);
1044*0Sstevel@tonic-gate 	for (msg = consms_mux_msg; msg != NULL; msg = msg->msg_next) {
1045*0Sstevel@tonic-gate 		if (msg->msg_id == msg_id)
1046*0Sstevel@tonic-gate 			break;
1047*0Sstevel@tonic-gate 	}
1048*0Sstevel@tonic-gate 	mutex_exit(&consms_msg_lock);
1049*0Sstevel@tonic-gate 
1050*0Sstevel@tonic-gate 	return (msg);
1051*0Sstevel@tonic-gate }
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate /*
1054*0Sstevel@tonic-gate  * Received ACK or NAK from lower mice
1055*0Sstevel@tonic-gate  *
1056*0Sstevel@tonic-gate  * For non-transparent ioctl, the msg->msg_rsp_list
1057*0Sstevel@tonic-gate  * is always NULL; for transparent ioctl, it
1058*0Sstevel@tonic-gate  * remembers the M_COPYIN/M_COPYOUT request
1059*0Sstevel@tonic-gate  * messages from lower mice. So here if msg->msg_rsp_list
1060*0Sstevel@tonic-gate  * is NULL (after receiving all ACK/NAKs), we
1061*0Sstevel@tonic-gate  * are done with this specific ioctl.
1062*0Sstevel@tonic-gate  *
1063*0Sstevel@tonic-gate  * As long as one of lower mice responds success,
1064*0Sstevel@tonic-gate  * we treat it success for a ioctl.
1065*0Sstevel@tonic-gate  */
1066*0Sstevel@tonic-gate static void
1067*0Sstevel@tonic-gate consms_mux_ack(consms_msg_t *msg, mblk_t *mp)
1068*0Sstevel@tonic-gate {
1069*0Sstevel@tonic-gate 	mblk_t	*ack_mp;
1070*0Sstevel@tonic-gate 
1071*0Sstevel@tonic-gate 	/* increment response_nums */
1072*0Sstevel@tonic-gate 	msg->msg_num_responses++;
1073*0Sstevel@tonic-gate 
1074*0Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_IOCACK) {
1075*0Sstevel@tonic-gate 		/*
1076*0Sstevel@tonic-gate 		 * Received ACK from lower, then
1077*0Sstevel@tonic-gate 		 * this is the last step for both
1078*0Sstevel@tonic-gate 		 * non-transparent and transparent
1079*0Sstevel@tonic-gate 		 * ioctl. We only need to remember
1080*0Sstevel@tonic-gate 		 * one of the ACKs, finally reply
1081*0Sstevel@tonic-gate 		 * this ACK to upper layer for this
1082*0Sstevel@tonic-gate 		 * specific ioctl.
1083*0Sstevel@tonic-gate 		 */
1084*0Sstevel@tonic-gate 		ASSERT(msg->msg_rsp_list == NULL);
1085*0Sstevel@tonic-gate 		if (msg->msg_ack_mp == NULL) {
1086*0Sstevel@tonic-gate 			msg->msg_ack_mp = mp;
1087*0Sstevel@tonic-gate 			mp = NULL;
1088*0Sstevel@tonic-gate 		}
1089*0Sstevel@tonic-gate 	}
1090*0Sstevel@tonic-gate 
1091*0Sstevel@tonic-gate 	/*
1092*0Sstevel@tonic-gate 	 * Check to see if all lower mice have responded
1093*0Sstevel@tonic-gate 	 * to our dispatching ioctl.
1094*0Sstevel@tonic-gate 	 */
1095*0Sstevel@tonic-gate 	if (msg->msg_num_responses == msg->msg_num_requests) {
1096*0Sstevel@tonic-gate 		if ((msg->msg_ack_mp == NULL) &&
1097*0Sstevel@tonic-gate 		    (msg->msg_rsp_list == NULL)) {
1098*0Sstevel@tonic-gate 			/*
1099*0Sstevel@tonic-gate 			 * All are NAKed.
1100*0Sstevel@tonic-gate 			 */
1101*0Sstevel@tonic-gate 			ack_mp = mp;
1102*0Sstevel@tonic-gate 			mp = NULL;
1103*0Sstevel@tonic-gate 		} else if (msg->msg_rsp_list == NULL) {
1104*0Sstevel@tonic-gate 			/*
1105*0Sstevel@tonic-gate 			 * The last step and at least one ACKed.
1106*0Sstevel@tonic-gate 			 */
1107*0Sstevel@tonic-gate 			ack_mp = msg->msg_ack_mp;
1108*0Sstevel@tonic-gate 			consms_mux_cache_states(msg->msg_request);
1109*0Sstevel@tonic-gate 			consms_mux_max_wheel_report(ack_mp);
1110*0Sstevel@tonic-gate 		} else {
1111*0Sstevel@tonic-gate 			/*
1112*0Sstevel@tonic-gate 			 * This is a NAK, but we have
1113*0Sstevel@tonic-gate 			 * already received M_COPYIN
1114*0Sstevel@tonic-gate 			 * or M_COPYOUT request from
1115*0Sstevel@tonic-gate 			 * at least one of lower mice.
1116*0Sstevel@tonic-gate 			 * (msg->msg_rsp_list != NULL)
1117*0Sstevel@tonic-gate 			 *
1118*0Sstevel@tonic-gate 			 * Still copyin or copyout.
1119*0Sstevel@tonic-gate 			 */
1120*0Sstevel@tonic-gate 			ack_mp = msg->msg_rsp_list->rsp_mp;
1121*0Sstevel@tonic-gate 			consms_mux_max_wheel_report(ack_mp);
1122*0Sstevel@tonic-gate 		}
1123*0Sstevel@tonic-gate 
1124*0Sstevel@tonic-gate 		qreply(msg->msg_queue, ack_mp);
1125*0Sstevel@tonic-gate 
1126*0Sstevel@tonic-gate 		if (msg->msg_rsp_list == NULL) {
1127*0Sstevel@tonic-gate 			/*
1128*0Sstevel@tonic-gate 			 * We are done with this ioctl.
1129*0Sstevel@tonic-gate 			 */
1130*0Sstevel@tonic-gate 			if (msg->msg_request)
1131*0Sstevel@tonic-gate 				freemsg(msg->msg_request);
1132*0Sstevel@tonic-gate 			(void) consms_mux_unlink_msg(msg->msg_id);
1133*0Sstevel@tonic-gate 			kmem_free(msg, sizeof (*msg));
1134*0Sstevel@tonic-gate 		}
1135*0Sstevel@tonic-gate 	}
1136*0Sstevel@tonic-gate 
1137*0Sstevel@tonic-gate 	if (mp) {
1138*0Sstevel@tonic-gate 		freemsg(mp);
1139*0Sstevel@tonic-gate 	}
1140*0Sstevel@tonic-gate }
1141*0Sstevel@tonic-gate 
1142*0Sstevel@tonic-gate /*
1143*0Sstevel@tonic-gate  * Received M_COPYIN or M_COPYOUT request from
1144*0Sstevel@tonic-gate  * lower mice for transparent ioctl
1145*0Sstevel@tonic-gate  *
1146*0Sstevel@tonic-gate  * We remember each M_COPYIN/M_COPYOUT into the
1147*0Sstevel@tonic-gate  * msg->msg_rsp_list, reply upper layer using the first
1148*0Sstevel@tonic-gate  * M_COPYIN/M_COPYOUT in the list after receiving
1149*0Sstevel@tonic-gate  * all responses from lower mice, even if some of
1150*0Sstevel@tonic-gate  * them return NAKs.
1151*0Sstevel@tonic-gate  */
1152*0Sstevel@tonic-gate static void
1153*0Sstevel@tonic-gate consms_mux_copyreq(queue_t *q, consms_msg_t *msg, mblk_t *mp)
1154*0Sstevel@tonic-gate {
1155*0Sstevel@tonic-gate 	consms_response_t	*rsp;
1156*0Sstevel@tonic-gate 
1157*0Sstevel@tonic-gate 	rsp = (consms_response_t *)kmem_zalloc(sizeof (*rsp), KM_SLEEP);
1158*0Sstevel@tonic-gate 	rsp->rsp_mp = mp;
1159*0Sstevel@tonic-gate 	rsp->rsp_queue = q;
1160*0Sstevel@tonic-gate 	if (msg->msg_rsp_list) {
1161*0Sstevel@tonic-gate 		rsp->rsp_next = msg->msg_rsp_list;
1162*0Sstevel@tonic-gate 	}
1163*0Sstevel@tonic-gate 	msg->msg_rsp_list = rsp;
1164*0Sstevel@tonic-gate 	msg->msg_num_responses++;
1165*0Sstevel@tonic-gate 
1166*0Sstevel@tonic-gate 	if (msg->msg_num_responses == msg->msg_num_requests) {
1167*0Sstevel@tonic-gate 		consms_mux_max_wheel_report(msg->msg_rsp_list->rsp_mp);
1168*0Sstevel@tonic-gate 		qreply(msg->msg_queue, msg->msg_rsp_list->rsp_mp);
1169*0Sstevel@tonic-gate 	}
1170*0Sstevel@tonic-gate }
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate /*
1173*0Sstevel@tonic-gate  * Do the real job for updating M_COPYIN/M_COPYOUT
1174*0Sstevel@tonic-gate  * request with the mp of M_IOCDATA, then put it
1175*0Sstevel@tonic-gate  * down to lower mice.
1176*0Sstevel@tonic-gate  */
1177*0Sstevel@tonic-gate static void
1178*0Sstevel@tonic-gate consms_mux_disp_iocdata(consms_response_t *rsp, mblk_t *mp)
1179*0Sstevel@tonic-gate {
1180*0Sstevel@tonic-gate 	mblk_t	*down_mp = rsp->rsp_mp;
1181*0Sstevel@tonic-gate 	struct copyresp *copyresp = (struct copyresp *)mp->b_rptr;
1182*0Sstevel@tonic-gate 	struct copyresp *newresp = (struct copyresp *)down_mp->b_rptr;
1183*0Sstevel@tonic-gate 
1184*0Sstevel@tonic-gate 	/*
1185*0Sstevel@tonic-gate 	 * Update the rval.
1186*0Sstevel@tonic-gate 	 */
1187*0Sstevel@tonic-gate 	newresp->cp_rval = copyresp->cp_rval;
1188*0Sstevel@tonic-gate 
1189*0Sstevel@tonic-gate 	/*
1190*0Sstevel@tonic-gate 	 * Update the db_type to M_IOCDATA.
1191*0Sstevel@tonic-gate 	 */
1192*0Sstevel@tonic-gate 	down_mp->b_datap->db_type = mp->b_datap->db_type;
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate 	/*
1195*0Sstevel@tonic-gate 	 * Update the b_cont.
1196*0Sstevel@tonic-gate 	 */
1197*0Sstevel@tonic-gate 	if (down_mp->b_cont != NULL) {
1198*0Sstevel@tonic-gate 		freemsg(down_mp->b_cont);
1199*0Sstevel@tonic-gate 		down_mp->b_cont = NULL;
1200*0Sstevel@tonic-gate 	}
1201*0Sstevel@tonic-gate 	if (mp->b_cont != NULL) {
1202*0Sstevel@tonic-gate 		down_mp->b_cont = copymsg(mp->b_cont);
1203*0Sstevel@tonic-gate 	}
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate 	/*
1206*0Sstevel@tonic-gate 	 * Put it down.
1207*0Sstevel@tonic-gate 	 */
1208*0Sstevel@tonic-gate 	(void) putq(WR(rsp->rsp_queue), down_mp);
1209*0Sstevel@tonic-gate }
1210*0Sstevel@tonic-gate 
1211*0Sstevel@tonic-gate /*
1212*0Sstevel@tonic-gate  * Dispatch M_IOCDATA down to all lower mice
1213*0Sstevel@tonic-gate  * for transparent ioctl.
1214*0Sstevel@tonic-gate  *
1215*0Sstevel@tonic-gate  * We update each M_COPYIN/M_COPYOUT in the
1216*0Sstevel@tonic-gate  * msg->msg_rsp_list with the M_IOCDATA.
1217*0Sstevel@tonic-gate  */
1218*0Sstevel@tonic-gate static void
1219*0Sstevel@tonic-gate consms_mux_iocdata(consms_msg_t *msg, mblk_t *mp)
1220*0Sstevel@tonic-gate {
1221*0Sstevel@tonic-gate 	consms_response_t	*rsp;
1222*0Sstevel@tonic-gate 	consms_response_t	*tmp;
1223*0Sstevel@tonic-gate 	consms_response_t	*first;
1224*0Sstevel@tonic-gate 	struct copyresp		*copyresp;
1225*0Sstevel@tonic-gate 	int			request_nums;
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate 	ASSERT(msg->msg_rsp_list != NULL);
1228*0Sstevel@tonic-gate 
1229*0Sstevel@tonic-gate 	/*
1230*0Sstevel@tonic-gate 	 * We should remember the ioc data for
1231*0Sstevel@tonic-gate 	 * VUIDSWHEELSTATE, and MSIOSRESOLUTION,
1232*0Sstevel@tonic-gate 	 * for we will cache the wheel state and
1233*0Sstevel@tonic-gate 	 * the screen resolution later if ACKed.
1234*0Sstevel@tonic-gate 	 */
1235*0Sstevel@tonic-gate 	copyresp = (struct copyresp *)mp->b_rptr;
1236*0Sstevel@tonic-gate 	if ((copyresp->cp_cmd == VUIDSWHEELSTATE) ||
1237*0Sstevel@tonic-gate 	    (copyresp->cp_cmd == MSIOSRESOLUTION)) {
1238*0Sstevel@tonic-gate 		freemsg(msg->msg_request);
1239*0Sstevel@tonic-gate 		msg->msg_request = copymsg(mp);
1240*0Sstevel@tonic-gate 	}
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate 	/*
1243*0Sstevel@tonic-gate 	 * Update request numbers and response numbers.
1244*0Sstevel@tonic-gate 	 */
1245*0Sstevel@tonic-gate 	msg->msg_num_requests = msg->msg_num_responses;
1246*0Sstevel@tonic-gate 	msg->msg_num_responses = 0;
1247*0Sstevel@tonic-gate 	request_nums = 1;
1248*0Sstevel@tonic-gate 
1249*0Sstevel@tonic-gate 	/*
1250*0Sstevel@tonic-gate 	 * Since we have use the first M_COPYIN/M_COPYOUT
1251*0Sstevel@tonic-gate 	 * in the msg_rsp_list to reply upper layer, the mp
1252*0Sstevel@tonic-gate 	 * of M_IOCDATA can be directly used for that.
1253*0Sstevel@tonic-gate 	 */
1254*0Sstevel@tonic-gate 	first = msg->msg_rsp_list;
1255*0Sstevel@tonic-gate 	rsp = first->rsp_next;
1256*0Sstevel@tonic-gate 	msg->msg_rsp_list = NULL;
1257*0Sstevel@tonic-gate 
1258*0Sstevel@tonic-gate 	for (rsp = first->rsp_next; rsp != NULL; ) {
1259*0Sstevel@tonic-gate 		tmp = rsp;
1260*0Sstevel@tonic-gate 		rsp = rsp->rsp_next;
1261*0Sstevel@tonic-gate 		consms_mux_disp_iocdata(tmp, mp);
1262*0Sstevel@tonic-gate 		kmem_free(tmp, sizeof (*tmp));
1263*0Sstevel@tonic-gate 		request_nums++;
1264*0Sstevel@tonic-gate 	}
1265*0Sstevel@tonic-gate 
1266*0Sstevel@tonic-gate 	/* Must set the request number before the last q. */
1267*0Sstevel@tonic-gate 	msg->msg_num_requests = request_nums;
1268*0Sstevel@tonic-gate 
1269*0Sstevel@tonic-gate 	/* the first one */
1270*0Sstevel@tonic-gate 	(void) putq(WR(first->rsp_queue), mp);
1271*0Sstevel@tonic-gate 	kmem_free(first, sizeof (*first));
1272*0Sstevel@tonic-gate }
1273*0Sstevel@tonic-gate 
1274*0Sstevel@tonic-gate 
1275*0Sstevel@tonic-gate /*
1276*0Sstevel@tonic-gate  * Here we update the number of wheels with
1277*0Sstevel@tonic-gate  * the virtual mouse for VUIDGWHEELCOUNT ioctl.
1278*0Sstevel@tonic-gate  */
1279*0Sstevel@tonic-gate static void
1280*0Sstevel@tonic-gate consms_mux_max_wheel_report(mblk_t *mp)
1281*0Sstevel@tonic-gate {
1282*0Sstevel@tonic-gate 	struct iocblk		*iocp;
1283*0Sstevel@tonic-gate 	int			num_wheels;
1284*0Sstevel@tonic-gate 
1285*0Sstevel@tonic-gate 	if (mp == NULL || mp->b_cont == NULL)
1286*0Sstevel@tonic-gate 		return;
1287*0Sstevel@tonic-gate 
1288*0Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
1289*0Sstevel@tonic-gate 
1290*0Sstevel@tonic-gate 	if ((iocp->ioc_cmd == VUIDGWHEELCOUNT) &&
1291*0Sstevel@tonic-gate 	    (mp->b_datap->db_type == M_COPYOUT)) {
1292*0Sstevel@tonic-gate 		num_wheels = *(int *)mp->b_cont->b_rptr;
1293*0Sstevel@tonic-gate 		if (num_wheels < consms_state.consms_num_wheels) {
1294*0Sstevel@tonic-gate 			*(int *)mp->b_cont->b_rptr =
1295*0Sstevel@tonic-gate 			    consms_state.consms_num_wheels;
1296*0Sstevel@tonic-gate 		}
1297*0Sstevel@tonic-gate 	}
1298*0Sstevel@tonic-gate }
1299*0Sstevel@tonic-gate 
1300*0Sstevel@tonic-gate /*
1301*0Sstevel@tonic-gate  * Update the virtual mouse state variables with
1302*0Sstevel@tonic-gate  * the latest value from upper layer when these
1303*0Sstevel@tonic-gate  * set ioctls return success. Thus we can update
1304*0Sstevel@tonic-gate  * low mice with the latest state values during
1305*0Sstevel@tonic-gate  * hotplug.
1306*0Sstevel@tonic-gate  */
1307*0Sstevel@tonic-gate static void
1308*0Sstevel@tonic-gate consms_mux_cache_states(mblk_t *mp)
1309*0Sstevel@tonic-gate {
1310*0Sstevel@tonic-gate 	struct iocblk 		*iocp;
1311*0Sstevel@tonic-gate 	Ms_parms		*parms;
1312*0Sstevel@tonic-gate 	Ms_screen_resolution	*sr;
1313*0Sstevel@tonic-gate 	wheel_state		*ws;
1314*0Sstevel@tonic-gate 
1315*0Sstevel@tonic-gate 	if (mp == NULL || mp->b_cont == NULL)
1316*0Sstevel@tonic-gate 		return;
1317*0Sstevel@tonic-gate 
1318*0Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
1319*0Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
1320*0Sstevel@tonic-gate 	case VUIDSFORMAT:
1321*0Sstevel@tonic-gate 		consms_state.consms_vuid_format = *(int *)mp->b_cont->b_rptr;
1322*0Sstevel@tonic-gate 		break;
1323*0Sstevel@tonic-gate 
1324*0Sstevel@tonic-gate 	case MSIOSETPARMS:
1325*0Sstevel@tonic-gate 		parms = (Ms_parms *)mp->b_cont->b_rptr;
1326*0Sstevel@tonic-gate 		consms_state.consms_ms_parms = *parms;
1327*0Sstevel@tonic-gate 		break;
1328*0Sstevel@tonic-gate 
1329*0Sstevel@tonic-gate 	case MSIOSRESOLUTION:
1330*0Sstevel@tonic-gate 		sr = (Ms_screen_resolution *)mp->b_cont->b_rptr;
1331*0Sstevel@tonic-gate 		consms_state.consms_ms_sr = *sr;
1332*0Sstevel@tonic-gate 		break;
1333*0Sstevel@tonic-gate 
1334*0Sstevel@tonic-gate 	case VUIDSWHEELSTATE:
1335*0Sstevel@tonic-gate 		ws = (wheel_state *)mp->b_cont->b_rptr;
1336*0Sstevel@tonic-gate 		consms_state.consms_wheel_state_bf =
1337*0Sstevel@tonic-gate 		    (ws->stateflags << ws->id) |
1338*0Sstevel@tonic-gate 		    (consms_state.consms_wheel_state_bf & ~(1 << ws->id));
1339*0Sstevel@tonic-gate 		break;
1340*0Sstevel@tonic-gate 	}
1341*0Sstevel@tonic-gate }
1342*0Sstevel@tonic-gate 
1343*0Sstevel@tonic-gate /*
1344*0Sstevel@tonic-gate  * Dispatch ioctl mp (non-transparent and transparent)
1345*0Sstevel@tonic-gate  * down to all lower mice.
1346*0Sstevel@tonic-gate  *
1347*0Sstevel@tonic-gate  * First, create a pending message for this mp, link it into
1348*0Sstevel@tonic-gate  * the global messages list. Then wait for ACK/NAK for
1349*0Sstevel@tonic-gate  * non-transparent ioctl, COPYIN/COPYOUT for transparent
1350*0Sstevel@tonic-gate  * ioctl.
1351*0Sstevel@tonic-gate  */
1352*0Sstevel@tonic-gate static int
1353*0Sstevel@tonic-gate consms_mux_disp_ioctl(queue_t *q, mblk_t *mp)
1354*0Sstevel@tonic-gate {
1355*0Sstevel@tonic-gate 	struct iocblk	*iocp;
1356*0Sstevel@tonic-gate 	consms_msg_t	*msg;
1357*0Sstevel@tonic-gate 	consms_lq_t	*lq;
1358*0Sstevel@tonic-gate 	mblk_t		*copy_mp;
1359*0Sstevel@tonic-gate 	int		error = 0;
1360*0Sstevel@tonic-gate 
1361*0Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
1362*0Sstevel@tonic-gate 	msg = (consms_msg_t *)kmem_zalloc(sizeof (*msg), KM_SLEEP);
1363*0Sstevel@tonic-gate 	msg->msg_id = iocp->ioc_id;
1364*0Sstevel@tonic-gate 	msg->msg_request = mp;
1365*0Sstevel@tonic-gate 	msg->msg_queue = q;
1366*0Sstevel@tonic-gate 	msg->msg_num_requests = consms_state.consms_num_lqs;
1367*0Sstevel@tonic-gate 	consms_mux_link_msg(msg);
1368*0Sstevel@tonic-gate 
1369*0Sstevel@tonic-gate 	for (lq = consms_state.consms_lqs; lq != NULL; lq = lq->lq_next) {
1370*0Sstevel@tonic-gate 		if ((copy_mp = copymsg(mp)) != NULL) {
1371*0Sstevel@tonic-gate 			(void) putq(lq->lq_queue, copy_mp);
1372*0Sstevel@tonic-gate 		} else {
1373*0Sstevel@tonic-gate 			/*
1374*0Sstevel@tonic-gate 			 * If copymsg fails, we ignore this lq and
1375*0Sstevel@tonic-gate 			 * try next one. As long as one of them succeeds,
1376*0Sstevel@tonic-gate 			 * we dispatch this ioctl down. And later as long
1377*0Sstevel@tonic-gate 			 * as one of the lower drivers return success, we
1378*0Sstevel@tonic-gate 			 * reply to this ioctl with success.
1379*0Sstevel@tonic-gate 			 */
1380*0Sstevel@tonic-gate 			msg->msg_num_requests--;
1381*0Sstevel@tonic-gate 		}
1382*0Sstevel@tonic-gate 	}
1383*0Sstevel@tonic-gate 
1384*0Sstevel@tonic-gate 	if (msg->msg_num_requests <= 0) {
1385*0Sstevel@tonic-gate 		/*
1386*0Sstevel@tonic-gate 		 * Since copymsg fails for all lqs, we NAK this ioctl.
1387*0Sstevel@tonic-gate 		 */
1388*0Sstevel@tonic-gate 		(void) consms_mux_unlink_msg(msg->msg_id);
1389*0Sstevel@tonic-gate 		kmem_free(msg, sizeof (*msg));
1390*0Sstevel@tonic-gate 		error = ENOMEM;
1391*0Sstevel@tonic-gate 	}
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 	return (error);
1394*0Sstevel@tonic-gate }
1395*0Sstevel@tonic-gate 
1396*0Sstevel@tonic-gate /*
1397*0Sstevel@tonic-gate  * Dispatch M_DATA and M_FLUSH message down to all
1398*0Sstevel@tonic-gate  * lower mice, and there are no acknowledgements
1399*0Sstevel@tonic-gate  * for them. Here we just copy the mp and then
1400*0Sstevel@tonic-gate  * put it into the lower queues.
1401*0Sstevel@tonic-gate  */
1402*0Sstevel@tonic-gate static void
1403*0Sstevel@tonic-gate consms_mux_disp_data(mblk_t *mp)
1404*0Sstevel@tonic-gate {
1405*0Sstevel@tonic-gate 	consms_lq_t	*lq;
1406*0Sstevel@tonic-gate 	mblk_t		*copy_mp;
1407*0Sstevel@tonic-gate 
1408*0Sstevel@tonic-gate 	for (lq = consms_state.consms_lqs; lq != NULL; lq = lq->lq_next) {
1409*0Sstevel@tonic-gate 		if ((copy_mp = copymsg(mp)) != NULL) {
1410*0Sstevel@tonic-gate 			(void) putq(lq->lq_queue, copy_mp);
1411*0Sstevel@tonic-gate 		}
1412*0Sstevel@tonic-gate 	}
1413*0Sstevel@tonic-gate 
1414*0Sstevel@tonic-gate 	freemsg(mp);
1415*0Sstevel@tonic-gate }
1416