1 /* $NetBSD: ms.c,v 1.37 2008/04/20 03:05:55 tsutsui Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)ms.c 8.1 (Berkeley) 6/11/93
44 * Mouse driver (/dev/mouse)
48 * Zilog Z8530 Dual UART driver (mouse interface)
50 * This is the "slave" driver that will be attached to
51 * the "zsc" driver for a Sun mouse.
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.37 2008/04/20 03:05:55 tsutsui Exp $");
57 #include <sys/param.h>
58 #include <sys/systm.h>
60 #include <sys/device.h>
61 #include <sys/ioctl.h>
62 #include <sys/kernel.h>
64 #include <sys/signal.h>
65 #include <sys/signalvar.h>
67 #include <sys/syslog.h>
68 #include <sys/select.h>
71 #include <machine/vuid_event.h>
73 #include <dev/ic/z8530reg.h>
74 #include <machine/z8530var.h>
75 #include <dev/sun/event_var.h>
76 #include <dev/sun/msvar.h>
78 #include <dev/wscons/wsconsio.h>
79 #include <dev/wscons/wsmousevar.h>
85 dev_type_open(msopen
);
86 dev_type_close(msclose
);
87 dev_type_read(msread
);
88 dev_type_ioctl(msioctl
);
89 dev_type_poll(mspoll
);
90 dev_type_kqfilter(mskqfilter
);
92 const struct cdevsw ms_cdevsw
= {
93 msopen
, msclose
, msread
, nowrite
, msioctl
,
94 nostop
, notty
, mspoll
, nommap
, mskqfilter
, D_OTHER
97 /****************************************************************
98 * Entry points for /dev/mouse
99 * (open,close,read,write,...)
100 ****************************************************************/
103 msopen(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
107 ms
= device_lookup_private(&ms_cd
, minor(dev
));
111 /* This is an exclusive open device. */
112 if (ms
->ms_events
.ev_io
)
115 if (ms
->ms_deviopen
) {
117 err
= (*ms
->ms_deviopen
)(ms
->ms_dev
, flags
);
121 ms
->ms_events
.ev_io
= l
->l_proc
;
122 ev_init(&ms
->ms_events
); /* may cause sleep */
124 ms
->ms_ready
= 1; /* start accepting events */
129 msclose(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
133 ms
= device_lookup_private(&ms_cd
, minor(dev
));
134 ms
->ms_ready
= 0; /* stop accepting events */
135 ev_fini(&ms
->ms_events
);
137 ms
->ms_events
.ev_io
= NULL
;
138 if (ms
->ms_deviclose
) {
140 err
= (*ms
->ms_deviclose
)(ms
->ms_dev
, flags
);
148 msread(dev_t dev
, struct uio
*uio
, int flags
)
152 ms
= device_lookup_private(&ms_cd
, minor(dev
));
153 return ev_read(&ms
->ms_events
, uio
, flags
);
157 msioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
161 ms
= device_lookup_private(&ms_cd
, minor(dev
));
165 case FIONBIO
: /* we will remove this someday (soon???) */
169 ms
->ms_events
.ev_async
= *(int *)data
!= 0;
173 if (-*(int *)data
!= ms
->ms_events
.ev_io
->p_pgid
174 && *(int *)data
!= ms
->ms_events
.ev_io
->p_pid
)
179 if (*(int *)data
!= ms
->ms_events
.ev_io
->p_pgid
)
184 /* we only do firm_events */
185 *(int *)data
= VUID_FIRM_EVENT
;
189 if (*(int *)data
!= VUID_FIRM_EVENT
)
197 mspoll(dev_t dev
, int events
, struct lwp
*l
)
201 ms
= device_lookup_private(&ms_cd
, minor(dev
));
202 return ev_poll(&ms
->ms_events
, events
, l
);
206 mskqfilter(dev_t dev
, struct knote
*kn
)
210 ms
= device_lookup_private(&ms_cd
, minor(dev
));
211 return ev_kqfilter(&ms
->ms_events
, kn
);
214 /****************************************************************
215 * Middle layer (translator)
216 ****************************************************************/
219 * Called by our ms_softint() routine on input.
222 ms_input(struct ms_softc
*ms
, int c
)
224 struct firm_event
*fe
;
225 int mb
, ub
, d
, get
, put
, any
;
226 static const char to_one
[] = { 1, 2, 2, 4, 4, 4, 4 };
227 static const int to_id
[] = { MS_RIGHT
, MS_MIDDLE
, 0, MS_LEFT
};
230 * Discard input if not ready. Drop sync on parity or framing
231 * error; gain sync on button byte.
233 if (ms
->ms_ready
== 0)
239 if ((c
& 0xb0) == 0x80) { /* if in 0x80..0x8f of 0xc0..0xcf */
241 ms
->ms_byteno
= 1; /* short form (3 bytes) */
243 ms
->ms_byteno
= 0; /* long form (5 bytes) */
248 * Run the decode loop, adding to the current information.
249 * We add, rather than replace, deltas, so that if the event queue
250 * fills, we accumulate data for when it opens up again.
252 switch (ms
->ms_byteno
) {
258 /* buttons (long form) */
260 ms
->ms_mb
= (~c
) & 0x7;
264 /* buttons (short form) */
266 ms
->ms_mb
= (~c
) & 0x7;
272 ms
->ms_dx
+= (char)c
;
278 ms
->ms_dy
+= (char)c
;
284 ms
->ms_dx
+= (char)c
;
289 ms
->ms_byteno
= -1; /* wait for button-byte again */
290 ms
->ms_dy
+= (char)c
;
299 if (ms
->ms_wsmousedev
!= NULL
&& ms
->ms_ready
== 2) {
300 mb
= ((ms
->ms_mb
& 4) >> 2) |
302 ((ms
->ms_mb
& 1) << 2);
303 wsmouse_input(ms
->ms_wsmousedev
,
305 ms
->ms_dx
, ms
->ms_dy
, 0, 0,
306 WSMOUSE_INPUT_DELTA
);
313 * We have at least one event (mouse button, delta-X, or
314 * delta-Y; possibly all three, and possibly three separate
315 * button events). Deliver these events until we are out
316 * of changes or out of room. As events get delivered,
317 * mark them `unchanged'.
320 get
= ms
->ms_events
.ev_get
;
321 put
= ms
->ms_events
.ev_put
;
322 fe
= &ms
->ms_events
.ev_q
[put
];
324 /* NEXT prepares to put the next event, backing off if necessary */
326 if ((++put) % EV_QSIZE == get) { \
330 /* ADVANCE completes the `put' of the event */
333 if (put >= EV_QSIZE) { \
335 fe = &ms->ms_events.ev_q[0]; \
341 while ((d
= mb
^ ub
) != 0) {
343 * Mouse button change. Convert up to three changes
344 * to the `first' change, and drop it into the event queue.
347 d
= to_one
[d
- 1]; /* from 1..7 to {1,2,4} */
348 fe
->id
= to_id
[d
- 1]; /* from {1,2,4} to ID */
349 fe
->value
= mb
& d
? VKEY_DOWN
: VKEY_UP
;
356 fe
->id
= LOC_X_DELTA
;
357 fe
->value
= ms
->ms_dx
;
364 fe
->id
= LOC_Y_DELTA
;
365 fe
->value
= ms
->ms_dy
;
373 ms
->ms_events
.ev_put
= put
;
374 EV_WAKEUP(&ms
->ms_events
);