xref: /onnv-gate/usr/src/uts/sun/io/ms.c (revision 0)
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 2003 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  * Mouse streams module.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/param.h>
35*0Sstevel@tonic-gate #include <sys/sysmacros.h>
36*0Sstevel@tonic-gate #include <sys/signal.h>
37*0Sstevel@tonic-gate #include <sys/termios.h>
38*0Sstevel@tonic-gate #include <sys/termio.h>
39*0Sstevel@tonic-gate #include <sys/stream.h>
40*0Sstevel@tonic-gate #include <sys/stropts.h>
41*0Sstevel@tonic-gate #include <sys/strsun.h>
42*0Sstevel@tonic-gate #include <sys/tty.h>
43*0Sstevel@tonic-gate #include <sys/strtty.h>
44*0Sstevel@tonic-gate #include <sys/time.h>
45*0Sstevel@tonic-gate #include <sys/kmem.h>
46*0Sstevel@tonic-gate #include <sys/file.h>
47*0Sstevel@tonic-gate #include <sys/uio.h>
48*0Sstevel@tonic-gate #include <sys/errno.h>
49*0Sstevel@tonic-gate #include <sys/debug.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include <sys/vuid_event.h>
52*0Sstevel@tonic-gate #include <sys/msreg.h>
53*0Sstevel@tonic-gate #include <sys/msio.h>
54*0Sstevel@tonic-gate #include <sys/ddi.h>
55*0Sstevel@tonic-gate #include <sys/sunddi.h>
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #include <sys/modctl.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * This is the loadable module wrapper.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static struct streamtab ms_info;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static struct fmodsw fsw = {
67*0Sstevel@tonic-gate 	"ms",
68*0Sstevel@tonic-gate 	&ms_info,
69*0Sstevel@tonic-gate 	D_MP | D_MTPERMOD
70*0Sstevel@tonic-gate };
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * Module linkage information for the kernel.
74*0Sstevel@tonic-gate  */
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
77*0Sstevel@tonic-gate 	&mod_strmodops, "streams module for mouse", &fsw
78*0Sstevel@tonic-gate };
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
81*0Sstevel@tonic-gate 	MODREV_1, &modlstrmod, NULL
82*0Sstevel@tonic-gate };
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate int
86*0Sstevel@tonic-gate _init(void)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate int
92*0Sstevel@tonic-gate _fini(void)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	return (EBUSY);
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate int
98*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate #define	BYTECLIP(x)	(char)((x) > 127 ? 127 : ((x) < -128 ? -128 : (x)))
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate struct msdata {
106*0Sstevel@tonic-gate 	struct ms_softc	msd_softc;
107*0Sstevel@tonic-gate 	queue_t	*msd_readq;	/* upstream read queue */
108*0Sstevel@tonic-gate 	mblk_t	*msd_iocpending; /* "ioctl" awaiting buffer */
109*0Sstevel@tonic-gate 	int	msd_flags;	/* random flags */
110*0Sstevel@tonic-gate 	int	msd_iocid;	/* ID of "ioctl" being waited for */
111*0Sstevel@tonic-gate 	int	msd_iocerror;	/* error return from "ioctl" */
112*0Sstevel@tonic-gate 	char	msd_oldbutt;	/* button state at last sample */
113*0Sstevel@tonic-gate 	short	msd_state;	/* state counter for input routine */
114*0Sstevel@tonic-gate 	short	msd_jitter;
115*0Sstevel@tonic-gate 	timeout_id_t	msd_timeout_id;	/* id returned by timeout() */
116*0Sstevel@tonic-gate 	bufcall_id_t	msd_reioctl_id;	/* id returned by bufcall() */
117*0Sstevel@tonic-gate 	bufcall_id_t	msd_resched_id;	/* id returned by bufcall() */
118*0Sstevel@tonic-gate 	int	msd_baud_rate;	/* mouse baud rate */
119*0Sstevel@tonic-gate 	int	msd_rcnt_baud_chng; /* baud changed recently */
120*0Sstevel@tonic-gate 	int	msd_data_pkt_cnt; /* no of packets since last baud change */
121*0Sstevel@tonic-gate 	int	msd_qenable_more; /* enable msrserv if baud changed recently */
122*0Sstevel@tonic-gate 	int	msd_hold_baud_stup; /* # of packets to wait for baud setup */
123*0Sstevel@tonic-gate };
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate #define	MS_OPEN		0x00000001	/* mouse is open for business */
126*0Sstevel@tonic-gate #define	MS_IOCWAIT	0x00000002	/* "open" waiting for ioctl to finish */
127*0Sstevel@tonic-gate #define	MS_IOCTOSS	0x00000004	/* Toss ioctl returns */
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate  * Input routine states. See msinput().
131*0Sstevel@tonic-gate  */
132*0Sstevel@tonic-gate #define	MS_WAIT_BUTN	0
133*0Sstevel@tonic-gate #define	MS_WAIT_X	1
134*0Sstevel@tonic-gate #define	MS_WAIT_Y	2
135*0Sstevel@tonic-gate #define	MS_WAIT_X2	3
136*0Sstevel@tonic-gate #define	MS_WAIT_Y2	4
137*0Sstevel@tonic-gate #define	MS_PKT_SZ	5
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate  * This module supports mice runing at 1200, 4800 and 9600 baud rates.
141*0Sstevel@tonic-gate  *
142*0Sstevel@tonic-gate  * If there was a baud change recently, then we want to wait
143*0Sstevel@tonic-gate  * for some time to make sure that no other baud change is on its way.
144*0Sstevel@tonic-gate  * If the second baud rate change is done then the packets between
145*0Sstevel@tonic-gate  * changes are garbage and are thrown away during the baud change.
146*0Sstevel@tonic-gate  */
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate  * The following #defines were tuned by experimentations.
149*0Sstevel@tonic-gate  */
150*0Sstevel@tonic-gate #define		MS_HOLD_BAUD_STUP	48
151*0Sstevel@tonic-gate #define		MS_CNT_TOB1200		7
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate static int	ms_overrun_msg;	/* Message when overrun circular buffer */
155*0Sstevel@tonic-gate static int	ms_overrun_cnt;	/* Increment when overrun circular buffer */
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * Max pixel delta of jitter controlled. As this number increases the jumpiness
159*0Sstevel@tonic-gate  * of the ms increases, i.e., the coarser the motion for medium speeds.
160*0Sstevel@tonic-gate  */
161*0Sstevel@tonic-gate static int	ms_jitter_thresh = 0;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate /*
164*0Sstevel@tonic-gate  * ms_jitter_thresh is the maximum number of jitters suppressed. Thus,
165*0Sstevel@tonic-gate  * hz/ms_jitter_thresh is the maximum interval of jitters suppressed. As
166*0Sstevel@tonic-gate  * ms_jitter_thresh increases, a wider range of jitter is suppressed. However,
167*0Sstevel@tonic-gate  * the more inertia the mouse seems to have, i.e., the slower the mouse is to
168*0Sstevel@tonic-gate  * react.
169*0Sstevel@tonic-gate  */
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate /*
172*0Sstevel@tonic-gate  * Measure how many (ms_speed_count) ms deltas exceed threshold
173*0Sstevel@tonic-gate  * (ms_speedlimit). If ms_speedlaw then throw away deltas over ms_speedlimit.
174*0Sstevel@tonic-gate  * This is to keep really bad mice that jump around from getting too far.
175*0Sstevel@tonic-gate  */
176*0Sstevel@tonic-gate static int	ms_speedlimit = 48;
177*0Sstevel@tonic-gate static int	ms_speedlaw = 0;
178*0Sstevel@tonic-gate static int	ms_speed_count;
179*0Sstevel@tonic-gate static int	msjitterrate = 12;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate #define	JITTER_TIMEOUT (hz/msjitterrate)
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate static clock_t	msjittertimeout; /* Timeout used when mstimeout in effect */
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate  * Mouse buffer size in bytes.  Place here as variable so that one could
187*0Sstevel@tonic-gate  * massage it using adb if it turns out to be too small.
188*0Sstevel@tonic-gate  */
189*0Sstevel@tonic-gate static int	MS_BUF_BYTES = 4096;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate static int	MS_DEBUG;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate /*
196*0Sstevel@tonic-gate  * Most of these should be "void", but the people who defined the "streams"
197*0Sstevel@tonic-gate  * data structures for S5 didn't understand data types.
198*0Sstevel@tonic-gate  */
199*0Sstevel@tonic-gate static int msopen(queue_t *q, dev_t *devp, int oflag, int sflag,
200*0Sstevel@tonic-gate 				cred_t *credp);
201*0Sstevel@tonic-gate static int msclose(queue_t *q, int flag, cred_t *credp);
202*0Sstevel@tonic-gate static void mswput(queue_t *q, mblk_t *mp);
203*0Sstevel@tonic-gate static void msrput(queue_t *q, mblk_t *mp);
204*0Sstevel@tonic-gate static void msrserv(queue_t *q);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate static struct module_info msmiinfo = {
207*0Sstevel@tonic-gate 	0,
208*0Sstevel@tonic-gate 	"ms",
209*0Sstevel@tonic-gate 	0,
210*0Sstevel@tonic-gate 	INFPSZ,
211*0Sstevel@tonic-gate 	2048,
212*0Sstevel@tonic-gate 	128
213*0Sstevel@tonic-gate };
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate static struct qinit msrinit = {
216*0Sstevel@tonic-gate 	(int (*)())msrput,
217*0Sstevel@tonic-gate 	(int (*)())msrserv,
218*0Sstevel@tonic-gate 	msopen,
219*0Sstevel@tonic-gate 	msclose,
220*0Sstevel@tonic-gate 	(int (*)())NULL,
221*0Sstevel@tonic-gate 	&msmiinfo
222*0Sstevel@tonic-gate };
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate static struct module_info msmoinfo = {
225*0Sstevel@tonic-gate 	0,
226*0Sstevel@tonic-gate 	"ms",
227*0Sstevel@tonic-gate 	0,
228*0Sstevel@tonic-gate 	INFPSZ,
229*0Sstevel@tonic-gate 	2048,
230*0Sstevel@tonic-gate 	128
231*0Sstevel@tonic-gate };
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate static struct qinit mswinit = {
234*0Sstevel@tonic-gate 	(int (*)())mswput,
235*0Sstevel@tonic-gate 	(int (*)())NULL,
236*0Sstevel@tonic-gate 	msopen,
237*0Sstevel@tonic-gate 	msclose,
238*0Sstevel@tonic-gate 	(int (*)())NULL,
239*0Sstevel@tonic-gate 	&msmoinfo
240*0Sstevel@tonic-gate };
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate static struct streamtab ms_info = {
243*0Sstevel@tonic-gate 	&msrinit,
244*0Sstevel@tonic-gate 	&mswinit,
245*0Sstevel@tonic-gate 	NULL,
246*0Sstevel@tonic-gate 	NULL,
247*0Sstevel@tonic-gate };
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate static void	msresched(void *);
250*0Sstevel@tonic-gate static void	msreioctl(void *);
251*0Sstevel@tonic-gate static void	msioctl(queue_t *q, mblk_t *mp);
252*0Sstevel@tonic-gate static int	ms_getparms(register Ms_parms *data);
253*0Sstevel@tonic-gate static int	ms_setparms(register Ms_parms *data);
254*0Sstevel@tonic-gate static void	msflush(struct msdata *msd);
255*0Sstevel@tonic-gate static void	msinput(/* struct msdata *msd, char c */); /* XXX */
256*0Sstevel@tonic-gate static void	msincr(void *);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate /*
259*0Sstevel@tonic-gate  * Dummy qbufcall callback routine used by open and close.
260*0Sstevel@tonic-gate  * The framework will wake up qwait_sig when we return from
261*0Sstevel@tonic-gate  * this routine (as part of leaving the perimeters.)
262*0Sstevel@tonic-gate  * (The framework enters the perimeters before calling the qbufcall() callback
263*0Sstevel@tonic-gate  * and leaves the perimeters after the callback routine has executed. The
264*0Sstevel@tonic-gate  * framework performs an implicit wakeup of any thread in qwait/qwait_sig
265*0Sstevel@tonic-gate  * when it leaves the perimeter. See qwait(9E).)
266*0Sstevel@tonic-gate  */
267*0Sstevel@tonic-gate /* ARGSUSED */
268*0Sstevel@tonic-gate static void
269*0Sstevel@tonic-gate dummy_callback(void *arg)
270*0Sstevel@tonic-gate {}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate /*
273*0Sstevel@tonic-gate  * Open a mouse.
274*0Sstevel@tonic-gate  */
275*0Sstevel@tonic-gate /*ARGSUSED*/
276*0Sstevel@tonic-gate static int
277*0Sstevel@tonic-gate msopen(q, devp, oflag, sflag, credp)
278*0Sstevel@tonic-gate 	queue_t	*q;
279*0Sstevel@tonic-gate 	dev_t	*devp;
280*0Sstevel@tonic-gate 	int	oflag, sflag;
281*0Sstevel@tonic-gate 	cred_t	*credp;
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate 	register struct mousebuf *b;
284*0Sstevel@tonic-gate 	register struct ms_softc *ms;
285*0Sstevel@tonic-gate 	register struct msdata *msd;
286*0Sstevel@tonic-gate 	mblk_t	 *mp;
287*0Sstevel@tonic-gate 	mblk_t	 *datap;
288*0Sstevel@tonic-gate 	register struct iocblk *iocb;
289*0Sstevel@tonic-gate 	register struct termios *cb;
290*0Sstevel@tonic-gate 	int error = 0;
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	if (q->q_ptr != NULL)
293*0Sstevel@tonic-gate 		return (0);		/* already attached */
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	if (sflag != MODOPEN)
296*0Sstevel@tonic-gate 		return (EINVAL);
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	/*
299*0Sstevel@tonic-gate 	 * Allocate an msdata structure.
300*0Sstevel@tonic-gate 	 */
301*0Sstevel@tonic-gate 	msd = kmem_zalloc(sizeof (struct msdata), KM_SLEEP);
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	/*
304*0Sstevel@tonic-gate 	 * Set up queue pointers, so that the "put" procedure will accept
305*0Sstevel@tonic-gate 	 * the reply to the "ioctl" message we send down.
306*0Sstevel@tonic-gate 	 */
307*0Sstevel@tonic-gate 	q->q_ptr = msd;
308*0Sstevel@tonic-gate 	WR(q)->q_ptr = msd;
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	qprocson(q);
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	/*
313*0Sstevel@tonic-gate 	 * Setup tty modes.
314*0Sstevel@tonic-gate 	 */
315*0Sstevel@tonic-gate 	while ((mp = mkiocb(TCSETSF)) == NULL) {
316*0Sstevel@tonic-gate 		bufcall_id_t id = qbufcall(q, sizeof (struct iocblk),
317*0Sstevel@tonic-gate 		    BPRI_HI, dummy_callback, NULL);
318*0Sstevel@tonic-gate 		if (!qwait_sig(q)) {
319*0Sstevel@tonic-gate 			qunbufcall(q, id);
320*0Sstevel@tonic-gate 			kmem_free(msd, sizeof (struct msdata));
321*0Sstevel@tonic-gate 			qprocsoff(q);
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 			return (EINTR);
324*0Sstevel@tonic-gate 		}
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 	while ((datap = allocb(sizeof (struct termios), BPRI_HI)) == NULL) {
327*0Sstevel@tonic-gate 		bufcall_id_t id = qbufcall(q, sizeof (struct termios),
328*0Sstevel@tonic-gate 		    BPRI_HI, dummy_callback, NULL);
329*0Sstevel@tonic-gate 		if (!qwait_sig(q)) {
330*0Sstevel@tonic-gate 			qunbufcall(q, id);
331*0Sstevel@tonic-gate 			freemsg(mp);
332*0Sstevel@tonic-gate 			kmem_free(msd, sizeof (struct msdata));
333*0Sstevel@tonic-gate 			qprocsoff(q);
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 			return (EINTR);
336*0Sstevel@tonic-gate 		}
337*0Sstevel@tonic-gate 	}
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	iocb = (struct iocblk *)mp->b_rptr;
341*0Sstevel@tonic-gate 	iocb->ioc_count = sizeof (struct termios);
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	cb = (struct termios *)datap->b_wptr;
344*0Sstevel@tonic-gate 	cb->c_iflag = 0;
345*0Sstevel@tonic-gate 	cb->c_oflag = 0;
346*0Sstevel@tonic-gate 	cb->c_cflag = CREAD|CS8|B9600;
347*0Sstevel@tonic-gate 	cb->c_lflag = 0;
348*0Sstevel@tonic-gate 	bzero(cb->c_cc, NCCS);
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	datap->b_wptr += sizeof (*cb);
351*0Sstevel@tonic-gate 	datap->b_datap->db_type = M_DATA;
352*0Sstevel@tonic-gate 	mp->b_cont = datap;
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	msd->msd_flags |= MS_IOCWAIT;	/* indicate that we're waiting for */
355*0Sstevel@tonic-gate 	msd->msd_iocid = iocb->ioc_id;	/* this response */
356*0Sstevel@tonic-gate 	msd->msd_baud_rate = B9600;
357*0Sstevel@tonic-gate 	msd->msd_rcnt_baud_chng = 1;
358*0Sstevel@tonic-gate 	msd->msd_data_pkt_cnt = 0;
359*0Sstevel@tonic-gate 	msd->msd_qenable_more = 0;
360*0Sstevel@tonic-gate 	msd->msd_hold_baud_stup = MS_HOLD_BAUD_STUP;
361*0Sstevel@tonic-gate 	putnext(WR(q), mp);
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	ms = &msd->msd_softc;
364*0Sstevel@tonic-gate 	/*
365*0Sstevel@tonic-gate 	 * Now wait for it.  Let our read queue put routine wake us up
366*0Sstevel@tonic-gate 	 * when it arrives.
367*0Sstevel@tonic-gate 	 */
368*0Sstevel@tonic-gate 	while (msd->msd_flags & MS_IOCWAIT) {
369*0Sstevel@tonic-gate 		if (!qwait_sig(q)) {
370*0Sstevel@tonic-gate 			error = EINTR;
371*0Sstevel@tonic-gate 			goto error;
372*0Sstevel@tonic-gate 		}
373*0Sstevel@tonic-gate 	}
374*0Sstevel@tonic-gate 	if ((error = msd->msd_iocerror) != 0)
375*0Sstevel@tonic-gate 		goto error;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	/*
378*0Sstevel@tonic-gate 	 * Set up private data.
379*0Sstevel@tonic-gate 	 */
380*0Sstevel@tonic-gate 	msd->msd_state = MS_WAIT_BUTN;
381*0Sstevel@tonic-gate 	msd->msd_readq = q;
382*0Sstevel@tonic-gate 	msd->msd_iocpending = NULL;
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	/*
385*0Sstevel@tonic-gate 	 * Allocate buffer and initialize data.
386*0Sstevel@tonic-gate 	 */
387*0Sstevel@tonic-gate 	if (ms->ms_buf == 0) {
388*0Sstevel@tonic-gate 		ms->ms_bufbytes = MS_BUF_BYTES;
389*0Sstevel@tonic-gate 		b = kmem_zalloc((uint_t)ms->ms_bufbytes, KM_SLEEP);
390*0Sstevel@tonic-gate 		b->mb_size = 1 + (ms->ms_bufbytes - sizeof (struct mousebuf))
391*0Sstevel@tonic-gate 			/ sizeof (struct mouseinfo);
392*0Sstevel@tonic-gate 		ms->ms_buf = b;
393*0Sstevel@tonic-gate 		ms->ms_vuidaddr = VKEY_FIRST;
394*0Sstevel@tonic-gate 		msjittertimeout = JITTER_TIMEOUT;
395*0Sstevel@tonic-gate 		msflush(msd);
396*0Sstevel@tonic-gate 	}
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	msd->msd_flags = MS_OPEN;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	/*
401*0Sstevel@tonic-gate 	 * Tell the module below us that it should return input immediately.
402*0Sstevel@tonic-gate 	 */
403*0Sstevel@tonic-gate 	(void) putnextctl1(WR(q), M_CTL, MC_SERVICEIMM);
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	return (0);
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate error:
408*0Sstevel@tonic-gate 	qprocsoff(q);
409*0Sstevel@tonic-gate 	kmem_free(msd, sizeof (struct msdata));
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	return (error);
412*0Sstevel@tonic-gate }
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate /*
415*0Sstevel@tonic-gate  * Close the mouse
416*0Sstevel@tonic-gate  */
417*0Sstevel@tonic-gate /* ARGSUSED1 */
418*0Sstevel@tonic-gate static int
419*0Sstevel@tonic-gate msclose(q, flag, credp)
420*0Sstevel@tonic-gate 	queue_t		*q;
421*0Sstevel@tonic-gate 	int		flag;
422*0Sstevel@tonic-gate 	cred_t		*credp;
423*0Sstevel@tonic-gate {
424*0Sstevel@tonic-gate 	register struct msdata *msd = (struct msdata *)q->q_ptr;
425*0Sstevel@tonic-gate 	register struct ms_softc *ms;
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	/*
428*0Sstevel@tonic-gate 	 * Tell the module below us that it need not return input immediately.
429*0Sstevel@tonic-gate 	 */
430*0Sstevel@tonic-gate 	(void) putnextctl1(q, M_CTL, MC_SERVICEDEF);
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	qprocsoff(q);
433*0Sstevel@tonic-gate 	/*
434*0Sstevel@tonic-gate 	 * Since we're about to destroy our private data, turn off
435*0Sstevel@tonic-gate 	 * our open flag first, so we don't accept any more input
436*0Sstevel@tonic-gate 	 * and try to use that data.
437*0Sstevel@tonic-gate 	 */
438*0Sstevel@tonic-gate 	msd->msd_flags = 0;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	if (msd->msd_jitter) {
441*0Sstevel@tonic-gate 		(void) quntimeout(q, msd->msd_timeout_id);
442*0Sstevel@tonic-gate 		msd->msd_jitter = 0;
443*0Sstevel@tonic-gate 	}
444*0Sstevel@tonic-gate 	if (msd->msd_reioctl_id) {
445*0Sstevel@tonic-gate 		qunbufcall(q, msd->msd_reioctl_id);
446*0Sstevel@tonic-gate 		msd->msd_reioctl_id = 0;
447*0Sstevel@tonic-gate 	}
448*0Sstevel@tonic-gate 	if (msd->msd_resched_id) {
449*0Sstevel@tonic-gate 		qunbufcall(q, msd->msd_resched_id);
450*0Sstevel@tonic-gate 		msd->msd_resched_id = 0;
451*0Sstevel@tonic-gate 	}
452*0Sstevel@tonic-gate 	if (msd->msd_iocpending != NULL) {
453*0Sstevel@tonic-gate 		/*
454*0Sstevel@tonic-gate 		 * We were holding an "ioctl" response pending the
455*0Sstevel@tonic-gate 		 * availability of an "mblk" to hold data to be passed up;
456*0Sstevel@tonic-gate 		 * another "ioctl" came through, which means that "ioctl"
457*0Sstevel@tonic-gate 		 * must have timed out or been aborted.
458*0Sstevel@tonic-gate 		 */
459*0Sstevel@tonic-gate 		freemsg(msd->msd_iocpending);
460*0Sstevel@tonic-gate 		msd->msd_iocpending = NULL;
461*0Sstevel@tonic-gate 	}
462*0Sstevel@tonic-gate 	ms = &msd->msd_softc;
463*0Sstevel@tonic-gate 	/* Free mouse buffer */
464*0Sstevel@tonic-gate 	if (ms->ms_buf != NULL)
465*0Sstevel@tonic-gate 		kmem_free(ms->ms_buf, (uint_t)ms->ms_bufbytes);
466*0Sstevel@tonic-gate 	/* Free msdata structure */
467*0Sstevel@tonic-gate 	kmem_free((void *)msd, sizeof (*msd));
468*0Sstevel@tonic-gate 	return (0);
469*0Sstevel@tonic-gate }
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate /*
472*0Sstevel@tonic-gate  * Read queue service routine.
473*0Sstevel@tonic-gate  * Turn buffered mouse events into stream messages.
474*0Sstevel@tonic-gate  */
475*0Sstevel@tonic-gate static void
476*0Sstevel@tonic-gate msrserv(q)
477*0Sstevel@tonic-gate 	register queue_t *q;
478*0Sstevel@tonic-gate {
479*0Sstevel@tonic-gate 	struct msdata *msd = (struct msdata *)q->q_ptr;
480*0Sstevel@tonic-gate 	register struct ms_softc *ms;
481*0Sstevel@tonic-gate 	register struct mousebuf *b;
482*0Sstevel@tonic-gate 	register struct mouseinfo *mi;
483*0Sstevel@tonic-gate 	register int    button_number;
484*0Sstevel@tonic-gate 	register int    hwbit;
485*0Sstevel@tonic-gate 	mblk_t	 *bp;
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 	/*
488*0Sstevel@tonic-gate 	 * Handle the case of a queue which is backenabled before
489*0Sstevel@tonic-gate 	 * initialization is complete.
490*0Sstevel@tonic-gate 	 */
491*0Sstevel@tonic-gate 	if (!(msd->msd_flags & MS_OPEN)) {
492*0Sstevel@tonic-gate 		return;
493*0Sstevel@tonic-gate 	}
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	ms = &msd->msd_softc;
496*0Sstevel@tonic-gate 	b = ms->ms_buf;
497*0Sstevel@tonic-gate 	if (msd->msd_rcnt_baud_chng && ms->ms_oldoff != b->mb_off) {
498*0Sstevel@tonic-gate 		int	no_pkt = b->mb_off - ms->ms_oldoff;
499*0Sstevel@tonic-gate 		int	i;
500*0Sstevel@tonic-gate 		no_pkt = no_pkt > 0 ? no_pkt : (b->mb_size - no_pkt);
501*0Sstevel@tonic-gate 		if (no_pkt < msd->msd_hold_baud_stup) {
502*0Sstevel@tonic-gate 			msd->msd_qenable_more = 1;
503*0Sstevel@tonic-gate 			return;
504*0Sstevel@tonic-gate 		} else {
505*0Sstevel@tonic-gate 			/*
506*0Sstevel@tonic-gate 			 * throw away packets in beginning (mostly garbage)
507*0Sstevel@tonic-gate 			 */
508*0Sstevel@tonic-gate 			for (i = 0; i < msd->msd_hold_baud_stup; i++) {
509*0Sstevel@tonic-gate 				ms->ms_oldoff++;	/* next event */
510*0Sstevel@tonic-gate 				/* circular buffer wraparound */
511*0Sstevel@tonic-gate 				if (ms->ms_oldoff >= b->mb_size)
512*0Sstevel@tonic-gate 					ms->ms_oldoff = 0;
513*0Sstevel@tonic-gate 			}
514*0Sstevel@tonic-gate 			msd->msd_rcnt_baud_chng = 0;
515*0Sstevel@tonic-gate 			msd->msd_data_pkt_cnt = 0;
516*0Sstevel@tonic-gate 			msd->msd_qenable_more = 0;
517*0Sstevel@tonic-gate 		}
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate 	while (canputnext(q) && ms->ms_oldoff != b->mb_off) {
520*0Sstevel@tonic-gate 		mi = &b->mb_info[ms->ms_oldoff];
521*0Sstevel@tonic-gate 		switch (ms->ms_readformat) {
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 		case MS_3BYTE_FORMAT: {
524*0Sstevel@tonic-gate 			register char *cp;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 			if ((bp = allocb(3, BPRI_HI)) != NULL) {
527*0Sstevel@tonic-gate 				cp = (char *)bp->b_wptr;
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 				*cp++ = 0x80 | mi->mi_buttons;
530*0Sstevel@tonic-gate 				/* Update read buttons */
531*0Sstevel@tonic-gate 				ms->ms_prevbuttons = mi->mi_buttons;
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 				*cp++ = mi->mi_x;
534*0Sstevel@tonic-gate 				*cp++ = -mi->mi_y;
535*0Sstevel@tonic-gate 				/* lower pri to avoid mouse droppings */
536*0Sstevel@tonic-gate 				bp->b_wptr = (uchar_t *)cp;
537*0Sstevel@tonic-gate 				putnext(q, bp);
538*0Sstevel@tonic-gate 			} else {
539*0Sstevel@tonic-gate 				if (msd->msd_resched_id)
540*0Sstevel@tonic-gate 					qunbufcall(q, msd->msd_resched_id);
541*0Sstevel@tonic-gate 				msd->msd_resched_id = qbufcall(q, 3, BPRI_HI,
542*0Sstevel@tonic-gate 				    msresched, msd);
543*0Sstevel@tonic-gate 				if (msd->msd_resched_id == 0)
544*0Sstevel@tonic-gate 					return;	/* try again later */
545*0Sstevel@tonic-gate 				/* bufcall failed; just pitch this event */
546*0Sstevel@tonic-gate 				/* or maybe flush queue? */
547*0Sstevel@tonic-gate 			}
548*0Sstevel@tonic-gate 			ms->ms_oldoff++;	/* next event */
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 			/* circular buffer wraparound */
551*0Sstevel@tonic-gate 			if (ms->ms_oldoff >= b->mb_size)
552*0Sstevel@tonic-gate 				ms->ms_oldoff = 0;
553*0Sstevel@tonic-gate 			break;
554*0Sstevel@tonic-gate 		}
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 		case MS_VUID_FORMAT: {
557*0Sstevel@tonic-gate 			register Firm_event *fep;
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 			bp = NULL;
560*0Sstevel@tonic-gate 			switch (ms->ms_eventstate) {
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 			case EVENT_BUT3:
563*0Sstevel@tonic-gate 			case EVENT_BUT2:
564*0Sstevel@tonic-gate 			case EVENT_BUT1:
565*0Sstevel@tonic-gate 			    /* Test the button. Send an event if it changed. */
566*0Sstevel@tonic-gate 			    button_number = ms->ms_eventstate - EVENT_BUT1;
567*0Sstevel@tonic-gate 			    hwbit = MS_HW_BUT1 >> button_number;
568*0Sstevel@tonic-gate 			    if ((ms->ms_prevbuttons & hwbit) !=
569*0Sstevel@tonic-gate 				(mi->mi_buttons & hwbit)) {
570*0Sstevel@tonic-gate 			    if ((bp = allocb(sizeof (Firm_event),
571*0Sstevel@tonic-gate 						BPRI_HI)) != NULL) {
572*0Sstevel@tonic-gate 				    fep = (Firm_event *)bp->b_wptr;
573*0Sstevel@tonic-gate 				    fep->id = vuid_id_addr(ms->ms_vuidaddr) |
574*0Sstevel@tonic-gate 					vuid_id_offset(BUT(1) + button_number);
575*0Sstevel@tonic-gate 				    fep->pair_type = FE_PAIR_NONE;
576*0Sstevel@tonic-gate 				    fep->pair = 0;
577*0Sstevel@tonic-gate 				    /* Update read buttons and set value */
578*0Sstevel@tonic-gate 				    if (mi->mi_buttons & hwbit) {
579*0Sstevel@tonic-gate 					fep->value = 0;
580*0Sstevel@tonic-gate 					ms->ms_prevbuttons |= hwbit;
581*0Sstevel@tonic-gate 				    } else {
582*0Sstevel@tonic-gate 					fep->value = 1;
583*0Sstevel@tonic-gate 					ms->ms_prevbuttons &= ~hwbit;
584*0Sstevel@tonic-gate 				    }
585*0Sstevel@tonic-gate 				    fep->time = mi->mi_time;
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 				} else {
588*0Sstevel@tonic-gate 				    if (msd->msd_resched_id)
589*0Sstevel@tonic-gate 					qunbufcall(q, msd->msd_resched_id);
590*0Sstevel@tonic-gate 				    msd->msd_resched_id = qbufcall(q,
591*0Sstevel@tonic-gate 					sizeof (Firm_event),
592*0Sstevel@tonic-gate 					BPRI_HI, msresched, msd);
593*0Sstevel@tonic-gate 				    if (msd->msd_resched_id == 0)
594*0Sstevel@tonic-gate 					return;	/* try again later */
595*0Sstevel@tonic-gate 				    /* bufcall failed; just pitch this event */
596*0Sstevel@tonic-gate 				    /* or maybe flush queue? */
597*0Sstevel@tonic-gate 				    ms->ms_eventstate = EVENT_X;
598*0Sstevel@tonic-gate 				}
599*0Sstevel@tonic-gate 			    }
600*0Sstevel@tonic-gate 			    break;
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 			case EVENT_Y:
603*0Sstevel@tonic-gate 			    /* Send y if changed. */
604*0Sstevel@tonic-gate 			    if (mi->mi_y != 0) {
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 				if ((bp = allocb(sizeof (Firm_event),
607*0Sstevel@tonic-gate 						BPRI_HI)) != NULL) {
608*0Sstevel@tonic-gate 				    fep = (Firm_event *)bp->b_wptr;
609*0Sstevel@tonic-gate 				    fep->id = vuid_id_addr(ms->ms_vuidaddr) |
610*0Sstevel@tonic-gate 					    vuid_id_offset(LOC_Y_DELTA);
611*0Sstevel@tonic-gate 				    fep->pair_type = FE_PAIR_ABSOLUTE;
612*0Sstevel@tonic-gate 				    fep->pair = (uchar_t)LOC_Y_ABSOLUTE;
613*0Sstevel@tonic-gate 				    fep->value = -mi->mi_y;
614*0Sstevel@tonic-gate 				    fep->time = mi->mi_time;
615*0Sstevel@tonic-gate 				} else {
616*0Sstevel@tonic-gate 				    if (msd->msd_resched_id)
617*0Sstevel@tonic-gate 					qunbufcall(q, msd->msd_resched_id);
618*0Sstevel@tonic-gate 				    msd->msd_resched_id = qbufcall(q,
619*0Sstevel@tonic-gate 					sizeof (Firm_event),
620*0Sstevel@tonic-gate 					BPRI_HI, msresched, msd);
621*0Sstevel@tonic-gate 				    if (msd->msd_resched_id == 0)
622*0Sstevel@tonic-gate 					return;	/* try again later */
623*0Sstevel@tonic-gate 				    /* bufcall failed; just pitch this event */
624*0Sstevel@tonic-gate 				    /* or maybe flush queue? */
625*0Sstevel@tonic-gate 				    ms->ms_eventstate = EVENT_X;
626*0Sstevel@tonic-gate 				}
627*0Sstevel@tonic-gate 			    }
628*0Sstevel@tonic-gate 			    break;
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 			case EVENT_X:
631*0Sstevel@tonic-gate 			    /* Send x if changed. */
632*0Sstevel@tonic-gate 			    if (mi->mi_x != 0) {
633*0Sstevel@tonic-gate 				if ((bp = allocb(sizeof (Firm_event),
634*0Sstevel@tonic-gate 						BPRI_HI)) != NULL) {
635*0Sstevel@tonic-gate 				    fep = (Firm_event *)bp->b_wptr;
636*0Sstevel@tonic-gate 				    fep->id = vuid_id_addr(ms->ms_vuidaddr) |
637*0Sstevel@tonic-gate 					    vuid_id_offset(LOC_X_DELTA);
638*0Sstevel@tonic-gate 				    fep->pair_type = FE_PAIR_ABSOLUTE;
639*0Sstevel@tonic-gate 				    fep->pair = (uchar_t)LOC_X_ABSOLUTE;
640*0Sstevel@tonic-gate 				    fep->value = mi->mi_x;
641*0Sstevel@tonic-gate 				    fep->time = mi->mi_time;
642*0Sstevel@tonic-gate 				} else {
643*0Sstevel@tonic-gate 				    if (msd->msd_resched_id)
644*0Sstevel@tonic-gate 					qunbufcall(q, msd->msd_resched_id);
645*0Sstevel@tonic-gate 				    msd->msd_resched_id = qbufcall(q,
646*0Sstevel@tonic-gate 					sizeof (Firm_event),
647*0Sstevel@tonic-gate 					BPRI_HI, msresched, msd);
648*0Sstevel@tonic-gate 				    if (msd->msd_resched_id == 0)
649*0Sstevel@tonic-gate 					return;	/* try again later */
650*0Sstevel@tonic-gate 				    /* bufcall failed; just pitch this event */
651*0Sstevel@tonic-gate 				    /* or maybe flush queue? */
652*0Sstevel@tonic-gate 				    ms->ms_eventstate = EVENT_X;
653*0Sstevel@tonic-gate 				}
654*0Sstevel@tonic-gate 			    }
655*0Sstevel@tonic-gate 			    break;
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 			}
658*0Sstevel@tonic-gate 			if (bp != NULL) {
659*0Sstevel@tonic-gate 			    /* lower pri to avoid mouse droppings */
660*0Sstevel@tonic-gate 			    bp->b_wptr += sizeof (Firm_event);
661*0Sstevel@tonic-gate 			    putnext(q, bp);
662*0Sstevel@tonic-gate 			}
663*0Sstevel@tonic-gate 			if (ms->ms_eventstate == EVENT_X) {
664*0Sstevel@tonic-gate 			    ms->ms_eventstate = EVENT_BUT3;
665*0Sstevel@tonic-gate 			    ms->ms_oldoff++;	/* next event */
666*0Sstevel@tonic-gate 
667*0Sstevel@tonic-gate 			    /* circular buffer wraparound */
668*0Sstevel@tonic-gate 			    if (ms->ms_oldoff >= b->mb_size)
669*0Sstevel@tonic-gate 				ms->ms_oldoff = 0;
670*0Sstevel@tonic-gate 			} else
671*0Sstevel@tonic-gate 			    ms->ms_eventstate--;
672*0Sstevel@tonic-gate 		}
673*0Sstevel@tonic-gate 		}
674*0Sstevel@tonic-gate 	}
675*0Sstevel@tonic-gate }
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate static void
678*0Sstevel@tonic-gate msresched(void *msdptr)
679*0Sstevel@tonic-gate {
680*0Sstevel@tonic-gate 	queue_t *q;
681*0Sstevel@tonic-gate 	struct msdata *msd = msdptr;
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 	msd->msd_resched_id = 0;
684*0Sstevel@tonic-gate 	if ((q = msd->msd_readq) != 0)
685*0Sstevel@tonic-gate 		qenable(q);	/* run the service procedure */
686*0Sstevel@tonic-gate }
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate /*
689*0Sstevel@tonic-gate  * Line discipline output queue put procedure: handles M_IOCTL
690*0Sstevel@tonic-gate  * messages.
691*0Sstevel@tonic-gate  */
692*0Sstevel@tonic-gate static void
693*0Sstevel@tonic-gate mswput(q, mp)
694*0Sstevel@tonic-gate 	register queue_t *q;
695*0Sstevel@tonic-gate 	register mblk_t *mp;
696*0Sstevel@tonic-gate {
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate 	/*
699*0Sstevel@tonic-gate 	 * Process M_FLUSH, and some M_IOCTL, messages here; pass
700*0Sstevel@tonic-gate 	 * everything else down.
701*0Sstevel@tonic-gate 	 */
702*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	case M_FLUSH:
705*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW)
706*0Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
707*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR)
708*0Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 	default:
711*0Sstevel@tonic-gate 		putnext(q, mp);	/* pass it down the line */
712*0Sstevel@tonic-gate 		break;
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 	case M_IOCTL:
715*0Sstevel@tonic-gate 		msioctl(q, mp);
716*0Sstevel@tonic-gate 		break;
717*0Sstevel@tonic-gate 	}
718*0Sstevel@tonic-gate }
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate static void
721*0Sstevel@tonic-gate msreioctl(void *msdptr)
722*0Sstevel@tonic-gate {
723*0Sstevel@tonic-gate 	struct msdata *msd = msdptr;
724*0Sstevel@tonic-gate 	queue_t *q;
725*0Sstevel@tonic-gate 	mblk_t *mp;
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 	msd->msd_reioctl_id = 0;
728*0Sstevel@tonic-gate 	q = msd->msd_readq;
729*0Sstevel@tonic-gate 	if ((mp = msd->msd_iocpending) != NULL) {
730*0Sstevel@tonic-gate 		msd->msd_iocpending = NULL;	/* not pending any more */
731*0Sstevel@tonic-gate 		msioctl(WR(q), mp);
732*0Sstevel@tonic-gate 	}
733*0Sstevel@tonic-gate }
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate static void
736*0Sstevel@tonic-gate msioctl(q, mp)
737*0Sstevel@tonic-gate 	register queue_t *q;
738*0Sstevel@tonic-gate 	register mblk_t *mp;
739*0Sstevel@tonic-gate {
740*0Sstevel@tonic-gate 	struct msdata		*msd;
741*0Sstevel@tonic-gate 	register struct ms_softc *ms;
742*0Sstevel@tonic-gate 	register struct iocblk	*iocp;
743*0Sstevel@tonic-gate 	Vuid_addr_probe		*addr_probe;
744*0Sstevel@tonic-gate 	uint_t			ioctlrespsize;
745*0Sstevel@tonic-gate 	int			err = 0;
746*0Sstevel@tonic-gate 	mblk_t			*datap;
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate 	msd = (struct msdata *)q->q_ptr;
749*0Sstevel@tonic-gate 	if (msd == NULL) {
750*0Sstevel@tonic-gate 		err = EINVAL;
751*0Sstevel@tonic-gate 		goto out;
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 	ms = &msd->msd_softc;
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	if (MS_DEBUG)
758*0Sstevel@tonic-gate 		printf("mswput(M_IOCTL,%x)\n", iocp->ioc_cmd);
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
761*0Sstevel@tonic-gate 	case VUIDSFORMAT:
762*0Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (int));
763*0Sstevel@tonic-gate 		if (err != 0)
764*0Sstevel@tonic-gate 			break;
765*0Sstevel@tonic-gate 		if (*(int *)mp->b_cont->b_rptr == ms->ms_readformat)
766*0Sstevel@tonic-gate 			break;
767*0Sstevel@tonic-gate 		ms->ms_readformat = *(int *)mp->b_cont->b_rptr;
768*0Sstevel@tonic-gate 		/*
769*0Sstevel@tonic-gate 		 * Flush mouse buffer because the messages upstream of us
770*0Sstevel@tonic-gate 		 * are in the old format.
771*0Sstevel@tonic-gate 		 */
772*0Sstevel@tonic-gate 		msflush(msd);
773*0Sstevel@tonic-gate 		break;
774*0Sstevel@tonic-gate 
775*0Sstevel@tonic-gate 	case VUIDGFORMAT:
776*0Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
777*0Sstevel@tonic-gate 			ioctlrespsize = sizeof (int);
778*0Sstevel@tonic-gate 			goto allocfailure;
779*0Sstevel@tonic-gate 		}
780*0Sstevel@tonic-gate 		*(int *)datap->b_wptr = ms->ms_readformat;
781*0Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
782*0Sstevel@tonic-gate 		if (mp->b_cont != NULL)
783*0Sstevel@tonic-gate 			freemsg(mp->b_cont);
784*0Sstevel@tonic-gate 		mp->b_cont = datap;
785*0Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
786*0Sstevel@tonic-gate 		break;
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	case VUIDSADDR:
789*0Sstevel@tonic-gate 	case VUIDGADDR:
790*0Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (Vuid_addr_probe));
791*0Sstevel@tonic-gate 		if (err != 0)
792*0Sstevel@tonic-gate 			break;
793*0Sstevel@tonic-gate 		addr_probe = (Vuid_addr_probe *)mp->b_cont->b_rptr;
794*0Sstevel@tonic-gate 		if (addr_probe->base != VKEY_FIRST) {
795*0Sstevel@tonic-gate 			err = ENODEV;
796*0Sstevel@tonic-gate 			break;
797*0Sstevel@tonic-gate 		}
798*0Sstevel@tonic-gate 		if (iocp->ioc_cmd == VUIDSADDR)
799*0Sstevel@tonic-gate 			ms->ms_vuidaddr = addr_probe->data.next;
800*0Sstevel@tonic-gate 		else
801*0Sstevel@tonic-gate 			addr_probe->data.current = ms->ms_vuidaddr;
802*0Sstevel@tonic-gate 		break;
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	case MSIOGETPARMS:
805*0Sstevel@tonic-gate 		if (MS_DEBUG)
806*0Sstevel@tonic-gate 			printf("ms_getparms\n");
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 		if ((datap = allocb(sizeof (Ms_parms), BPRI_HI)) == NULL) {
809*0Sstevel@tonic-gate 			ioctlrespsize = sizeof (Ms_parms);
810*0Sstevel@tonic-gate 			goto allocfailure;
811*0Sstevel@tonic-gate 		}
812*0Sstevel@tonic-gate 		err = ms_getparms((Ms_parms *)datap->b_wptr);
813*0Sstevel@tonic-gate 		datap->b_wptr += sizeof (Ms_parms);
814*0Sstevel@tonic-gate 		if (mp->b_cont != NULL)
815*0Sstevel@tonic-gate 			freemsg(mp->b_cont);
816*0Sstevel@tonic-gate 		mp->b_cont = datap;
817*0Sstevel@tonic-gate 		iocp->ioc_count = sizeof (Ms_parms);
818*0Sstevel@tonic-gate 		break;
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	case MSIOSETPARMS:
821*0Sstevel@tonic-gate 		if (MS_DEBUG)
822*0Sstevel@tonic-gate 			printf("ms_setparms\n");
823*0Sstevel@tonic-gate 
824*0Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (Ms_parms));
825*0Sstevel@tonic-gate 		if (err != 0)
826*0Sstevel@tonic-gate 			break;
827*0Sstevel@tonic-gate 		err = ms_setparms((Ms_parms *)mp->b_cont->b_rptr);
828*0Sstevel@tonic-gate 		break;
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 	default:
831*0Sstevel@tonic-gate 		putnext(q, mp);	/* pass it down the line */
832*0Sstevel@tonic-gate 		return;
833*0Sstevel@tonic-gate 	}
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate out:
836*0Sstevel@tonic-gate 	if (err != 0)
837*0Sstevel@tonic-gate 		miocnak(q, mp, 0, err);
838*0Sstevel@tonic-gate 	else {
839*0Sstevel@tonic-gate 		iocp->ioc_rval = 0;
840*0Sstevel@tonic-gate 		iocp->ioc_error = 0;	/* brain rot */
841*0Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
842*0Sstevel@tonic-gate 		qreply(q, mp);
843*0Sstevel@tonic-gate 	}
844*0Sstevel@tonic-gate 	return;
845*0Sstevel@tonic-gate 
846*0Sstevel@tonic-gate allocfailure:
847*0Sstevel@tonic-gate 	/*
848*0Sstevel@tonic-gate 	 * We needed to allocate something to handle this "ioctl", but
849*0Sstevel@tonic-gate 	 * couldn't; save this "ioctl" and arrange to get called back when
850*0Sstevel@tonic-gate 	 * it's more likely that we can get what we need.
851*0Sstevel@tonic-gate 	 * If there's already one being saved, throw it out, since it
852*0Sstevel@tonic-gate 	 * must have timed out.
853*0Sstevel@tonic-gate 	 */
854*0Sstevel@tonic-gate 	if (msd->msd_iocpending != NULL)
855*0Sstevel@tonic-gate 		freemsg(msd->msd_iocpending);
856*0Sstevel@tonic-gate 	msd->msd_iocpending = mp;
857*0Sstevel@tonic-gate 	if (msd->msd_reioctl_id)
858*0Sstevel@tonic-gate 		qunbufcall(q, msd->msd_reioctl_id);
859*0Sstevel@tonic-gate 	msd->msd_reioctl_id = qbufcall(q, ioctlrespsize, BPRI_HI,
860*0Sstevel@tonic-gate 	    msreioctl, msd);
861*0Sstevel@tonic-gate }
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate static int
864*0Sstevel@tonic-gate ms_getparms(data)
865*0Sstevel@tonic-gate 	register Ms_parms	*data;
866*0Sstevel@tonic-gate {
867*0Sstevel@tonic-gate 	data->jitter_thresh = ms_jitter_thresh;
868*0Sstevel@tonic-gate 	data->speed_law = ms_speedlaw;
869*0Sstevel@tonic-gate 	data->speed_limit = ms_speedlimit;
870*0Sstevel@tonic-gate 	return (0);
871*0Sstevel@tonic-gate }
872*0Sstevel@tonic-gate 
873*0Sstevel@tonic-gate static int
874*0Sstevel@tonic-gate ms_setparms(data)
875*0Sstevel@tonic-gate 	register Ms_parms	*data;
876*0Sstevel@tonic-gate {
877*0Sstevel@tonic-gate 	ms_jitter_thresh = data->jitter_thresh;
878*0Sstevel@tonic-gate 	ms_speedlaw = data->speed_law;
879*0Sstevel@tonic-gate 	ms_speedlimit = data->speed_limit;
880*0Sstevel@tonic-gate 	return (0);
881*0Sstevel@tonic-gate }
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate static void
884*0Sstevel@tonic-gate msflush(msd)
885*0Sstevel@tonic-gate 	register struct msdata *msd;
886*0Sstevel@tonic-gate {
887*0Sstevel@tonic-gate 	register struct ms_softc *ms = &msd->msd_softc;
888*0Sstevel@tonic-gate 	register queue_t *q;
889*0Sstevel@tonic-gate 
890*0Sstevel@tonic-gate 	ms->ms_oldoff = 0;
891*0Sstevel@tonic-gate 	ms->ms_eventstate = EVENT_BUT3;
892*0Sstevel@tonic-gate 	ms->ms_buf->mb_off = 0;
893*0Sstevel@tonic-gate 	ms->ms_prevbuttons = MS_HW_BUT1 | MS_HW_BUT2 | MS_HW_BUT3;
894*0Sstevel@tonic-gate 	msd->msd_oldbutt = ms->ms_prevbuttons;
895*0Sstevel@tonic-gate 	if ((q = msd->msd_readq) != NULL && q->q_next != NULL)
896*0Sstevel@tonic-gate 		(void) putnextctl1(q, M_FLUSH, FLUSHR);
897*0Sstevel@tonic-gate }
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate /*
901*0Sstevel@tonic-gate  * Mouse read queue put procedure.
902*0Sstevel@tonic-gate  */
903*0Sstevel@tonic-gate static void
904*0Sstevel@tonic-gate msrput(q, mp)
905*0Sstevel@tonic-gate 	register queue_t *q;
906*0Sstevel@tonic-gate 	register mblk_t *mp;
907*0Sstevel@tonic-gate {
908*0Sstevel@tonic-gate 	register struct msdata *msd = (struct msdata *)q->q_ptr;
909*0Sstevel@tonic-gate 	register mblk_t *bp;
910*0Sstevel@tonic-gate 	register char *readp;
911*0Sstevel@tonic-gate 	register mblk_t *imp;
912*0Sstevel@tonic-gate 	register mblk_t *datap;
913*0Sstevel@tonic-gate 	register struct iocblk *iocb;
914*0Sstevel@tonic-gate 	register struct termios *cb;
915*0Sstevel@tonic-gate 	struct iocblk *iocp;
916*0Sstevel@tonic-gate 
917*0Sstevel@tonic-gate 	if (msd == 0)
918*0Sstevel@tonic-gate 		return;
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate 	case M_FLUSH:
923*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW)
924*0Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
925*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR)
926*0Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
927*0Sstevel@tonic-gate 
928*0Sstevel@tonic-gate 	default:
929*0Sstevel@tonic-gate 		putnext(q, mp);
930*0Sstevel@tonic-gate 		return;
931*0Sstevel@tonic-gate 
932*0Sstevel@tonic-gate 	case M_BREAK:
933*0Sstevel@tonic-gate 		if (msd->msd_flags & MS_IOCTOSS) {
934*0Sstevel@tonic-gate 			freemsg(mp);
935*0Sstevel@tonic-gate 			return;
936*0Sstevel@tonic-gate 		}
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 		if (msd->msd_rcnt_baud_chng && msd->msd_data_pkt_cnt == 0) {
939*0Sstevel@tonic-gate 			freemsg(mp);
940*0Sstevel@tonic-gate 			return;
941*0Sstevel@tonic-gate 		}
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 		/*
944*0Sstevel@tonic-gate 		 * If we are sampling a 4800 baud mouse at 9600,
945*0Sstevel@tonic-gate 		 * we want to wait for long time because there is no
946*0Sstevel@tonic-gate 		 * fixed timeframe for receiving break. If we are sampling
947*0Sstevel@tonic-gate 		 * a 1200 baud mouse at 4800 or 9600 baud rate then
948*0Sstevel@tonic-gate 		 * it is guaranteed that break will be received very soon.
949*0Sstevel@tonic-gate 		 */
950*0Sstevel@tonic-gate 		if (msd->msd_rcnt_baud_chng) {
951*0Sstevel@tonic-gate 			switch (msd->msd_baud_rate) {
952*0Sstevel@tonic-gate 			case B9600:
953*0Sstevel@tonic-gate 				msd->msd_hold_baud_stup = MS_HOLD_BAUD_STUP/2;
954*0Sstevel@tonic-gate 				msd->msd_baud_rate = B4800;
955*0Sstevel@tonic-gate 				break;
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate 			case B4800:
958*0Sstevel@tonic-gate 				if (msd->msd_data_pkt_cnt <= MS_CNT_TOB1200) {
959*0Sstevel@tonic-gate 					msd->msd_hold_baud_stup =
960*0Sstevel@tonic-gate 						MS_HOLD_BAUD_STUP/6;
961*0Sstevel@tonic-gate 					msd->msd_baud_rate = B1200;
962*0Sstevel@tonic-gate 				} else {
963*0Sstevel@tonic-gate 					msd->msd_hold_baud_stup =
964*0Sstevel@tonic-gate 						MS_HOLD_BAUD_STUP;
965*0Sstevel@tonic-gate 					msd->msd_baud_rate = B9600;
966*0Sstevel@tonic-gate 				}
967*0Sstevel@tonic-gate 				break;
968*0Sstevel@tonic-gate 
969*0Sstevel@tonic-gate 			case B1200:
970*0Sstevel@tonic-gate 			default:
971*0Sstevel@tonic-gate 				msd->msd_hold_baud_stup = MS_HOLD_BAUD_STUP;
972*0Sstevel@tonic-gate 				msd->msd_baud_rate = B9600;
973*0Sstevel@tonic-gate 				break;
974*0Sstevel@tonic-gate 			}
975*0Sstevel@tonic-gate 		} else {
976*0Sstevel@tonic-gate 			msd->msd_hold_baud_stup = MS_HOLD_BAUD_STUP;
977*0Sstevel@tonic-gate 			msd->msd_baud_rate = B9600;
978*0Sstevel@tonic-gate 		}
979*0Sstevel@tonic-gate 
980*0Sstevel@tonic-gate 		/*
981*0Sstevel@tonic-gate 		 * Change baud rate.
982*0Sstevel@tonic-gate 		 */
983*0Sstevel@tonic-gate 		if ((imp = mkiocb(TCSETSF)) == NULL) {
984*0Sstevel@tonic-gate 			return;
985*0Sstevel@tonic-gate 		}
986*0Sstevel@tonic-gate 		if ((datap = allocb(sizeof (struct termios),
987*0Sstevel@tonic-gate 		    BPRI_HI)) == NULL) {
988*0Sstevel@tonic-gate 			freemsg(imp);
989*0Sstevel@tonic-gate 			return;
990*0Sstevel@tonic-gate 		}
991*0Sstevel@tonic-gate 
992*0Sstevel@tonic-gate 		iocb = (struct iocblk *)imp->b_rptr;
993*0Sstevel@tonic-gate 		iocb->ioc_count = sizeof (struct termios);
994*0Sstevel@tonic-gate 
995*0Sstevel@tonic-gate 		cb = (struct termios *)datap->b_rptr;
996*0Sstevel@tonic-gate 		cb->c_iflag = 0;
997*0Sstevel@tonic-gate 		cb->c_oflag = 0;
998*0Sstevel@tonic-gate 		cb->c_cflag = CREAD|CS8|msd->msd_baud_rate;
999*0Sstevel@tonic-gate 		cb->c_lflag = 0;
1000*0Sstevel@tonic-gate 		bzero(cb->c_cc, NCCS);
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 		datap->b_wptr += sizeof (*cb);
1003*0Sstevel@tonic-gate 		datap->b_datap->db_type = M_DATA;
1004*0Sstevel@tonic-gate 		imp->b_cont = datap;
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 		msd->msd_flags |= MS_IOCTOSS|MS_IOCWAIT;
1007*0Sstevel@tonic-gate 		msd->msd_iocid = iocb->ioc_id;
1008*0Sstevel@tonic-gate 		msflush(msd);
1009*0Sstevel@tonic-gate 		flushq(q, FLUSHALL);
1010*0Sstevel@tonic-gate 		putnext(WR(q), imp);
1011*0Sstevel@tonic-gate 		freemsg(mp);
1012*0Sstevel@tonic-gate 		msd->msd_rcnt_baud_chng = 1;
1013*0Sstevel@tonic-gate 		msd->msd_data_pkt_cnt = 0;
1014*0Sstevel@tonic-gate 		if (MS_DEBUG)
1015*0Sstevel@tonic-gate 			printf("baud %x\n", msd->msd_baud_rate);
1016*0Sstevel@tonic-gate 		return;
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 	case M_IOCACK:
1019*0Sstevel@tonic-gate 	case M_IOCNAK:
1020*0Sstevel@tonic-gate 		/*
1021*0Sstevel@tonic-gate 		 * If we are doing an "ioctl" ourselves, check if this
1022*0Sstevel@tonic-gate 		 * is the reply to that code.  If so, wake up the
1023*0Sstevel@tonic-gate 		 * "open" routine, and toss the reply, otherwise just
1024*0Sstevel@tonic-gate 		 * pass it up.
1025*0Sstevel@tonic-gate 		 */
1026*0Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
1027*0Sstevel@tonic-gate 		if (!(msd->msd_flags & MS_IOCWAIT) ||
1028*0Sstevel@tonic-gate 		    iocp->ioc_id != msd->msd_iocid) {
1029*0Sstevel@tonic-gate 			/*
1030*0Sstevel@tonic-gate 			 * This isn't the reply we're looking for.  Move along.
1031*0Sstevel@tonic-gate 			 */
1032*0Sstevel@tonic-gate 			putnext(q, mp);
1033*0Sstevel@tonic-gate 		} else {
1034*0Sstevel@tonic-gate 			msd->msd_flags &= ~MS_IOCWAIT;
1035*0Sstevel@tonic-gate 			msd->msd_iocerror = iocp->ioc_error;
1036*0Sstevel@tonic-gate 			/*
1037*0Sstevel@tonic-gate 			 * If we sent down a request to change the baud rate.
1038*0Sstevel@tonic-gate 			 * This is the reply.  Just ignore it.
1039*0Sstevel@tonic-gate 			 */
1040*0Sstevel@tonic-gate 			if (msd->msd_flags & MS_IOCTOSS) {
1041*0Sstevel@tonic-gate 				msd->msd_flags &= ~MS_IOCTOSS;
1042*0Sstevel@tonic-gate 				msflush(msd);
1043*0Sstevel@tonic-gate 				flushq(q, FLUSHALL);
1044*0Sstevel@tonic-gate 			}
1045*0Sstevel@tonic-gate 			freemsg(mp);
1046*0Sstevel@tonic-gate 		}
1047*0Sstevel@tonic-gate 		return;
1048*0Sstevel@tonic-gate 
1049*0Sstevel@tonic-gate 	case M_DATA:
1050*0Sstevel@tonic-gate 		if ((msd->msd_flags & MS_IOCTOSS) ||
1051*0Sstevel@tonic-gate 		    !(msd->msd_flags & MS_OPEN)) {
1052*0Sstevel@tonic-gate 			freemsg(mp);
1053*0Sstevel@tonic-gate 			return;
1054*0Sstevel@tonic-gate 		}
1055*0Sstevel@tonic-gate 		break;
1056*0Sstevel@tonic-gate 	}
1057*0Sstevel@tonic-gate 
1058*0Sstevel@tonic-gate 	/*
1059*0Sstevel@tonic-gate 	 * A data message, consisting of bytes from the mouse.
1060*0Sstevel@tonic-gate 	 * Hand each byte to our input routine.
1061*0Sstevel@tonic-gate 	 */
1062*0Sstevel@tonic-gate 	bp = mp;
1063*0Sstevel@tonic-gate 
1064*0Sstevel@tonic-gate 	do {
1065*0Sstevel@tonic-gate 		readp = (char *)bp->b_rptr;
1066*0Sstevel@tonic-gate 		while (readp < (char *)bp->b_wptr) {
1067*0Sstevel@tonic-gate 			if (msd->msd_rcnt_baud_chng)
1068*0Sstevel@tonic-gate 				msd->msd_data_pkt_cnt++;
1069*0Sstevel@tonic-gate 			msinput(msd, *readp++);
1070*0Sstevel@tonic-gate 		}
1071*0Sstevel@tonic-gate 		bp->b_rptr = (unsigned char *)readp;
1072*0Sstevel@tonic-gate 	} while ((bp = bp->b_cont) != NULL);	/* next block, if any */
1073*0Sstevel@tonic-gate 
1074*0Sstevel@tonic-gate 	freemsg(mp);
1075*0Sstevel@tonic-gate }
1076*0Sstevel@tonic-gate 
1077*0Sstevel@tonic-gate /*
1078*0Sstevel@tonic-gate  * Mouse input routine; process a byte received from a mouse and
1079*0Sstevel@tonic-gate  * assemble into a mouseinfo message for the window system.
1080*0Sstevel@tonic-gate  *
1081*0Sstevel@tonic-gate  * The MSC mice send a five-byte packet organized as
1082*0Sstevel@tonic-gate  *	button, dx, dy, dx, dy
1083*0Sstevel@tonic-gate  * where dx and dy can be any signed byte value. The mouseinfo message
1084*0Sstevel@tonic-gate  * is organized as
1085*0Sstevel@tonic-gate  *	dx, dy, button, timestamp
1086*0Sstevel@tonic-gate  * Our strategy is to add up the 2 dx and the 2 dy in the five-byte
1087*0Sstevel@tonic-gate  * packet, then send the mouseinfo message up.
1088*0Sstevel@tonic-gate  *
1089*0Sstevel@tonic-gate  * Basic algorithm: throw away bytes until we get a [potential]
1090*0Sstevel@tonic-gate  * button byte. Collect button; Collect dx1; Collect dy1; Collect dx2
1091*0Sstevel@tonic-gate  * and add it to dx1; Collect dy2 and add it to dy1; Send button,
1092*0Sstevel@tonic-gate  * dx, dy, timestamp.
1093*0Sstevel@tonic-gate  *
1094*0Sstevel@tonic-gate  * Watch out for overflow!
1095*0Sstevel@tonic-gate  */
1096*0Sstevel@tonic-gate 
1097*0Sstevel@tonic-gate static void
1098*0Sstevel@tonic-gate msinput(msd, c)
1099*0Sstevel@tonic-gate 	register struct msdata *msd;
1100*0Sstevel@tonic-gate 	char c;
1101*0Sstevel@tonic-gate {
1102*0Sstevel@tonic-gate 	register struct ms_softc *ms;
1103*0Sstevel@tonic-gate 	register struct mousebuf *b;
1104*0Sstevel@tonic-gate 	register struct mouseinfo *mi;
1105*0Sstevel@tonic-gate 	register int    jitter_radius;
1106*0Sstevel@tonic-gate 	register int    temp;
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate 	ms = &msd->msd_softc;
1109*0Sstevel@tonic-gate 	b = ms->ms_buf;
1110*0Sstevel@tonic-gate 	if (b == NULL)
1111*0Sstevel@tonic-gate 		return;
1112*0Sstevel@tonic-gate 
1113*0Sstevel@tonic-gate 	mi = &b->mb_info[b->mb_off];
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate 	switch (msd->msd_state) {
1116*0Sstevel@tonic-gate 
1117*0Sstevel@tonic-gate 	case MS_WAIT_BUTN:
1118*0Sstevel@tonic-gate 		if ((c & 0xf8) != 0x80) {
1119*0Sstevel@tonic-gate 			if (MS_DEBUG)
1120*0Sstevel@tonic-gate 				printf("Mouse input char %x discarded\n",
1121*0Sstevel@tonic-gate 					(int)c & 0xff);
1122*0Sstevel@tonic-gate 			if (msd->msd_rcnt_baud_chng) {
1123*0Sstevel@tonic-gate 				msflush(msd);
1124*0Sstevel@tonic-gate 				flushq(msd->msd_readq, FLUSHALL);
1125*0Sstevel@tonic-gate 				msd->msd_hold_baud_stup++;
1126*0Sstevel@tonic-gate 			}
1127*0Sstevel@tonic-gate 			return;
1128*0Sstevel@tonic-gate 		}
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate 		/*
1131*0Sstevel@tonic-gate 		 * Probably a button byte.
1132*0Sstevel@tonic-gate 		 * Lower 3 bits are left, middle, right.
1133*0Sstevel@tonic-gate 		 */
1134*0Sstevel@tonic-gate 		mi->mi_buttons = c & (MS_HW_BUT1 | MS_HW_BUT2 | MS_HW_BUT3);
1135*0Sstevel@tonic-gate 		break;
1136*0Sstevel@tonic-gate 
1137*0Sstevel@tonic-gate 	case MS_WAIT_X:
1138*0Sstevel@tonic-gate 		/*
1139*0Sstevel@tonic-gate 		 * Delta X byte.  Add the delta X from this sample to
1140*0Sstevel@tonic-gate 		 * the delta X we're accumulating in the current event.
1141*0Sstevel@tonic-gate 		 */
1142*0Sstevel@tonic-gate 		temp = (int)(mi->mi_x + c);
1143*0Sstevel@tonic-gate 		mi->mi_x = BYTECLIP(temp);
1144*0Sstevel@tonic-gate 		uniqtime32(&mi->mi_time); /* record time when sample arrived */
1145*0Sstevel@tonic-gate 		break;
1146*0Sstevel@tonic-gate 
1147*0Sstevel@tonic-gate 	case MS_WAIT_Y:
1148*0Sstevel@tonic-gate 		/*
1149*0Sstevel@tonic-gate 		 * Delta Y byte.  Add the delta Y from this sample to
1150*0Sstevel@tonic-gate 		 * the delta Y we're accumulating in the current event.
1151*0Sstevel@tonic-gate 		 * (Subtract, actually, because the mouse reports
1152*0Sstevel@tonic-gate 		 * increasing Y up the screen.)
1153*0Sstevel@tonic-gate 		 */
1154*0Sstevel@tonic-gate 		temp = (int)(mi->mi_y - c);
1155*0Sstevel@tonic-gate 		mi->mi_y = BYTECLIP(temp);
1156*0Sstevel@tonic-gate 		break;
1157*0Sstevel@tonic-gate 
1158*0Sstevel@tonic-gate 	case MS_WAIT_X2:
1159*0Sstevel@tonic-gate 		/*
1160*0Sstevel@tonic-gate 		 * Second delta X byte.
1161*0Sstevel@tonic-gate 		 */
1162*0Sstevel@tonic-gate 		temp = (int)(mi->mi_x + c);
1163*0Sstevel@tonic-gate 		mi->mi_x = BYTECLIP(temp);
1164*0Sstevel@tonic-gate 		uniqtime32(&mi->mi_time);
1165*0Sstevel@tonic-gate 		break;
1166*0Sstevel@tonic-gate 
1167*0Sstevel@tonic-gate 	case MS_WAIT_Y2:
1168*0Sstevel@tonic-gate 		/*
1169*0Sstevel@tonic-gate 		 * Second delta Y byte.
1170*0Sstevel@tonic-gate 		 */
1171*0Sstevel@tonic-gate 		temp = (int)(mi->mi_y - c);
1172*0Sstevel@tonic-gate 		mi->mi_y = BYTECLIP(temp);
1173*0Sstevel@tonic-gate 		break;
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate 	}
1176*0Sstevel@tonic-gate 
1177*0Sstevel@tonic-gate 	/*
1178*0Sstevel@tonic-gate 	 * Done yet?
1179*0Sstevel@tonic-gate 	 */
1180*0Sstevel@tonic-gate 	if (msd->msd_state == MS_WAIT_Y2)
1181*0Sstevel@tonic-gate 		msd->msd_state = MS_WAIT_BUTN;	/* BONG. Start again. */
1182*0Sstevel@tonic-gate 	else {
1183*0Sstevel@tonic-gate 		msd->msd_state += 1;
1184*0Sstevel@tonic-gate 		return;
1185*0Sstevel@tonic-gate 	}
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 	if (msd->msd_jitter) {
1188*0Sstevel@tonic-gate 		(void) quntimeout(msd->msd_readq, msd->msd_timeout_id);
1189*0Sstevel@tonic-gate 		msd->msd_jitter = 0;
1190*0Sstevel@tonic-gate 	}
1191*0Sstevel@tonic-gate 
1192*0Sstevel@tonic-gate 	if (mi->mi_buttons == msd->msd_oldbutt) {
1193*0Sstevel@tonic-gate 		/*
1194*0Sstevel@tonic-gate 		 * Buttons did not change; did position?
1195*0Sstevel@tonic-gate 		 */
1196*0Sstevel@tonic-gate 		if (mi->mi_x == 0 && mi->mi_y == 0) {
1197*0Sstevel@tonic-gate 			/* no, position did not change - boring event */
1198*0Sstevel@tonic-gate 			return;
1199*0Sstevel@tonic-gate 		}
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 		/*
1202*0Sstevel@tonic-gate 		 * Did the mouse move more than the jitter threshhold?
1203*0Sstevel@tonic-gate 		 */
1204*0Sstevel@tonic-gate 		jitter_radius = ms_jitter_thresh;
1205*0Sstevel@tonic-gate 		if (ABS((int)mi->mi_x) <= jitter_radius &&
1206*0Sstevel@tonic-gate 		    ABS((int)mi->mi_y) <= jitter_radius) {
1207*0Sstevel@tonic-gate 			/*
1208*0Sstevel@tonic-gate 			 * Mouse moved less than the jitter threshhold.
1209*0Sstevel@tonic-gate 			 * Don't indicate an event; keep accumulating motions.
1210*0Sstevel@tonic-gate 			 * After "msjittertimeout" ticks expire, treat
1211*0Sstevel@tonic-gate 			 * the accumulated delta as the real delta.
1212*0Sstevel@tonic-gate 			 */
1213*0Sstevel@tonic-gate 			msd->msd_jitter = 1;
1214*0Sstevel@tonic-gate 			msd->msd_timeout_id = qtimeout(msd->msd_readq,
1215*0Sstevel@tonic-gate 			    msincr, msd, msjittertimeout);
1216*0Sstevel@tonic-gate 			return;
1217*0Sstevel@tonic-gate 		}
1218*0Sstevel@tonic-gate 	}
1219*0Sstevel@tonic-gate 	msd->msd_oldbutt = mi->mi_buttons;
1220*0Sstevel@tonic-gate 	msincr(msd);
1221*0Sstevel@tonic-gate }
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate /*
1224*0Sstevel@tonic-gate  * Increment the mouse sample pointer.
1225*0Sstevel@tonic-gate  * Called either immediately after a sample or after a jitter timeout.
1226*0Sstevel@tonic-gate  */
1227*0Sstevel@tonic-gate static void
1228*0Sstevel@tonic-gate msincr(void *arg)
1229*0Sstevel@tonic-gate {
1230*0Sstevel@tonic-gate 	struct msdata  *msd = arg;
1231*0Sstevel@tonic-gate 	register struct ms_softc *ms = &msd->msd_softc;
1232*0Sstevel@tonic-gate 	register struct mousebuf *b;
1233*0Sstevel@tonic-gate 	register struct mouseinfo *mi;
1234*0Sstevel@tonic-gate 	char			oldbutt;
1235*0Sstevel@tonic-gate 	register short		xc, yc;
1236*0Sstevel@tonic-gate 	register int		wake;
1237*0Sstevel@tonic-gate 	register int		speedlimit = ms_speedlimit;
1238*0Sstevel@tonic-gate 	register int		xabs, yabs;
1239*0Sstevel@tonic-gate 
1240*0Sstevel@tonic-gate 	/*
1241*0Sstevel@tonic-gate 	 * No longer waiting for jitter timeout
1242*0Sstevel@tonic-gate 	 */
1243*0Sstevel@tonic-gate 	msd->msd_jitter = 0;
1244*0Sstevel@tonic-gate 
1245*0Sstevel@tonic-gate 	b = ms->ms_buf;
1246*0Sstevel@tonic-gate 	if (b == NULL)
1247*0Sstevel@tonic-gate 		return;
1248*0Sstevel@tonic-gate 	mi = &b->mb_info[b->mb_off];
1249*0Sstevel@tonic-gate 
1250*0Sstevel@tonic-gate 	if (ms_speedlaw) {
1251*0Sstevel@tonic-gate 		xabs = ABS((int)mi->mi_x);
1252*0Sstevel@tonic-gate 		yabs = ABS((int)mi->mi_y);
1253*0Sstevel@tonic-gate 		if (xabs > speedlimit || yabs > speedlimit)
1254*0Sstevel@tonic-gate 			ms_speed_count++;
1255*0Sstevel@tonic-gate 		if (xabs > speedlimit)
1256*0Sstevel@tonic-gate 			mi->mi_x = 0;
1257*0Sstevel@tonic-gate 		if (yabs > speedlimit)
1258*0Sstevel@tonic-gate 			mi->mi_y = 0;
1259*0Sstevel@tonic-gate 	}
1260*0Sstevel@tonic-gate 
1261*0Sstevel@tonic-gate 	oldbutt = mi->mi_buttons;
1262*0Sstevel@tonic-gate 
1263*0Sstevel@tonic-gate 	xc = yc = 0;
1264*0Sstevel@tonic-gate 
1265*0Sstevel@tonic-gate 	/* See if we need to wake up anyone waiting for input */
1266*0Sstevel@tonic-gate 	wake = b->mb_off == ms->ms_oldoff;
1267*0Sstevel@tonic-gate 
1268*0Sstevel@tonic-gate 	/* Adjust circular buffer pointer */
1269*0Sstevel@tonic-gate 	if (++b->mb_off >= b->mb_size) {
1270*0Sstevel@tonic-gate 		b->mb_off = 0;
1271*0Sstevel@tonic-gate 		mi = b->mb_info;
1272*0Sstevel@tonic-gate 	} else {
1273*0Sstevel@tonic-gate 		mi++;
1274*0Sstevel@tonic-gate 	}
1275*0Sstevel@tonic-gate 
1276*0Sstevel@tonic-gate 	/*
1277*0Sstevel@tonic-gate 	 * If over-took read index then flush buffer so that mouse state
1278*0Sstevel@tonic-gate 	 * is consistent.
1279*0Sstevel@tonic-gate 	 */
1280*0Sstevel@tonic-gate 	if (b->mb_off == ms->ms_oldoff) {
1281*0Sstevel@tonic-gate 		if (ms_overrun_msg)
1282*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
1283*0Sstevel@tonic-gate 				"Mouse buffer flushed when overrun.\n");
1284*0Sstevel@tonic-gate 		msflush(msd);
1285*0Sstevel@tonic-gate 		ms_overrun_cnt++;
1286*0Sstevel@tonic-gate 		mi = b->mb_info;
1287*0Sstevel@tonic-gate 	}
1288*0Sstevel@tonic-gate 
1289*0Sstevel@tonic-gate 	/* Remember current buttons and fractional part of x & y */
1290*0Sstevel@tonic-gate 	mi->mi_buttons = oldbutt;
1291*0Sstevel@tonic-gate 	mi->mi_x = (char)xc;
1292*0Sstevel@tonic-gate 	mi->mi_y = (char)yc;
1293*0Sstevel@tonic-gate 	if (wake || msd->msd_qenable_more)
1294*0Sstevel@tonic-gate 		qenable(msd->msd_readq);	/* run the service procedure */
1295*0Sstevel@tonic-gate }
1296