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