1 /* $NetBSD: ms.c,v 1.23 2009/03/14 15:36:03 dsl Exp $ */
4 * Copyright (c) 1995 Leo Weppelman.
9 * Copyright (c) 1992, 1993
10 * The Regents of the University of California. All rights reserved.
12 * This software was developed by the Computer Systems Engineering group
13 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
14 * contributed to Berkeley.
16 * All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Lawrence Berkeley Laboratory.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * @(#)ms.c 8.1 (Berkeley) 6/11/93
47 * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp (LBL)
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.23 2009/03/14 15:36:03 dsl Exp $");
57 #include <sys/param.h>
59 #include <sys/ioctl.h>
60 #include <sys/kernel.h>
62 #include <sys/systm.h>
63 #include <sys/callout.h>
65 #include <sys/signalvar.h>
67 #include <machine/msioctl.h>
68 #include <atari/dev/event_var.h>
69 #include <atari/dev/vuid_event.h>
70 #include <atari/dev/kbdvar.h>
71 #include <atari/dev/msvar.h>
76 /* there's really no more physical ports on an atari. */
82 typedef void (*FPV
)(void *);
84 static struct ms_softc ms_softc
[NMOUSE
];
86 dev_type_open(msopen
);
87 dev_type_close(msclose
);
88 dev_type_read(msread
);
89 dev_type_ioctl(msioctl
);
90 dev_type_poll(mspoll
);
91 dev_type_kqfilter(mskqfilter
);
93 const struct cdevsw ms_cdevsw
= {
94 msopen
, msclose
, msread
, nowrite
, msioctl
,
95 nostop
, notty
, mspoll
, nommap
, mskqfilter
,
98 static void ms_3b_delay(struct ms_softc
*);
103 printf("1 mouse configured\n");
104 ms_softc
[0].ms_emul3b
= 1;
105 callout_init(&ms_softc
[0].ms_delay_ch
, 0);
110 ms_3b_delay(struct ms_softc
*ms
)
114 rel_ms
.id
= TIMEOUT_ID
;
115 rel_ms
.dx
= rel_ms
.dy
= 0;
116 mouse_soft(&rel_ms
, sizeof(rel_ms
), KBD_TIMEO_PKG
);
119 * Note that we are called from the keyboard software interrupt!
122 mouse_soft(REL_MOUSE
*rel_ms
, int size
, int type
)
124 struct ms_softc
*ms
= &ms_softc
[0];
125 struct firm_event
*fe
, *fe2
;
126 REL_MOUSE fake_mouse
;
132 if (ms
->ms_events
.ev_io
== NULL
)
138 * Ignore if in emulation mode
144 * There are some mice that have their middle button
145 * wired to the 'up' bit of joystick 1....
146 * Simulate a mouse packet with dx = dy = 0, the middle
147 * button state set by UP and the other buttons unchanged.
148 * Flush all button changes.
151 fake_mouse
.id
= (rel_ms
->dx
& 1 ? 4 : 0) | (ms
->ms_buttons
& 3);
152 fake_mouse
.dx
= fake_mouse
.dy
= 0;
153 rel_ms
= &fake_mouse
;
157 * Timeout package. No button changes and no movement.
158 * Flush all button changes.
161 fake_mouse
.id
= ms
->ms_buttons
;
162 fake_mouse
.dx
= fake_mouse
.dy
= 0;
163 rel_ms
= &fake_mouse
;
167 * Normal mouse package. Always copy the middle button
168 * status. The emulation code decides if button changes
171 rel_ms
->id
= (ms
->ms_buttons
& 4) | (rel_ms
->id
& 3);
172 flush_buttons
= (ms
->ms_emul3b
) ? 0 : 1;
179 get
= ms
->ms_events
.ev_get
;
180 put
= ms
->ms_events
.ev_put
;
181 fe
= &ms
->ms_events
.ev_q
[put
];
183 if ((type
!= KBD_TIMEO_PKG
) && ms
->ms_emul3b
&& ms
->ms_bq_idx
)
184 callout_stop(&ms
->ms_delay_ch
);
187 * Button states are encoded in the lower 3 bits of 'id'
189 if (!(mbut
= (rel_ms
->id
^ ms
->ms_buttons
)) && (put
!= get
)) {
191 * Compact dx/dy messages. Always generate an event when
192 * a button is pressed or the event queue is empty.
194 ms
->ms_dx
+= rel_ms
->dx
;
195 ms
->ms_dy
+= rel_ms
->dy
;
198 rel_ms
->dx
+= ms
->ms_dx
;
199 rel_ms
->dy
+= ms
->ms_dy
;
200 ms
->ms_dx
= ms
->ms_dy
= 0;
203 * Output location events _before_ button events ie. make sure
204 * the button is pressed at the correct location.
207 if ((++put
) % EV_QSIZE
== get
) {
211 fe
->id
= LOC_X_DELTA
;
212 fe
->value
= rel_ms
->dx
;
214 if (put
>= EV_QSIZE
) {
216 fe
= &ms
->ms_events
.ev_q
[0];
221 if ((++put
) % EV_QSIZE
== get
) {
225 fe
->id
= LOC_Y_DELTA
;
226 fe
->value
= rel_ms
->dy
;
228 if (put
>= EV_QSIZE
) {
230 fe
= &ms
->ms_events
.ev_q
[0];
234 if (mbut
&& (type
!= KBD_TIMEO_PKG
)) {
235 for (bmask
= 1; bmask
< 0x08; bmask
<<= 1) {
238 fe2
= &ms
->ms_bq
[ms
->ms_bq_idx
++];
243 else fe2
->id
= MS_MIDDLE
;
244 fe2
->value
= rel_ms
->id
& bmask
? VKEY_DOWN
: VKEY_UP
;
250 * Handle 3rd button emulation.
252 if (ms
->ms_emul3b
&& ms
->ms_bq_idx
&& (type
!= KBD_TIMEO_PKG
)) {
254 * If the middle button is pressed, any change to
255 * one of the other buttons releases all.
257 if ((ms
->ms_buttons
& 4) && (mbut
& 3)) {
258 ms
->ms_bq
[0].id
= MS_MIDDLE
;
264 if (ms
->ms_bq_idx
== 2) {
265 if (ms
->ms_bq
[0].value
== ms
->ms_bq
[1].value
) {
266 /* Must be 2 button presses! */
267 ms
->ms_bq
[0].id
= MS_MIDDLE
;
272 else if (ms
->ms_bq
[0].value
== VKEY_DOWN
) {
273 callout_reset(&ms
->ms_delay_ch
, 10,
274 (FPV
)ms_3b_delay
, (void *)ms
);
283 for (i
= 0; i
< ms
->ms_bq_idx
; i
++) {
284 if ((++put
) % EV_QSIZE
== get
) {
290 if (put
>= EV_QSIZE
) {
292 fe
= &ms
->ms_events
.ev_q
[0];
298 ms
->ms_events
.ev_put
= put
;
299 ms
->ms_buttons
= rel_ms
->id
;
301 EV_WAKEUP(&ms
->ms_events
);
305 msopen(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
307 u_char report_ms_joy
[] = { 0x14, 0x08 };
312 ms
= &ms_softc
[unit
];
317 if (ms
->ms_events
.ev_io
)
320 ms
->ms_events
.ev_io
= l
->l_proc
;
321 ms
->ms_dx
= ms
->ms_dy
= 0;
323 ms
->ms_bq
[0].id
= ms
->ms_bq
[1].id
= 0;
325 ev_init(&ms
->ms_events
); /* may cause sleep */
328 * Enable mouse reporting.
330 kbd_write(report_ms_joy
, sizeof(report_ms_joy
));
335 msclose(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
337 u_char disable_ms_joy
[] = { 0x12, 0x1a };
342 ms
= &ms_softc
[unit
];
345 * Turn off mouse interrogation.
347 kbd_write(disable_ms_joy
, sizeof(disable_ms_joy
));
348 ev_fini(&ms
->ms_events
);
349 ms
->ms_events
.ev_io
= NULL
;
354 msread(dev_t dev
, struct uio
*uio
, int flags
)
358 ms
= &ms_softc
[minor(dev
)];
359 return(ev_read(&ms
->ms_events
, uio
, flags
));
363 msioctl(dev_t dev
, u_long cmd
, register void * data
, int flag
, struct lwp
*l
)
369 ms
= &ms_softc
[unit
];
373 ms
->ms_emul3b
= (*(int *)data
!= 0) ? 1 : 0;
376 *(int *)data
= ms
->ms_emul3b
;
378 case FIONBIO
: /* we will remove this someday (soon???) */
381 ms
->ms_events
.ev_async
= *(int *)data
!= 0;
384 if (-*(int *)data
!= ms
->ms_events
.ev_io
->p_pgid
385 && *(int *)data
!= ms
->ms_events
.ev_io
->p_pid
)
389 if (*(int *)data
!= ms
->ms_events
.ev_io
->p_pgid
)
392 case VUIDGFORMAT
: /* we only do firm_events */
393 *(int *)data
= VUID_FIRM_EVENT
;
396 if (*(int *)data
!= VUID_FIRM_EVENT
)
404 mspoll(dev_t dev
, int events
, struct lwp
*l
)
408 ms
= &ms_softc
[minor(dev
)];
409 return(ev_poll(&ms
->ms_events
, events
, l
));
413 mskqfilter(dev_t dev
, struct knote
*kn
)
417 ms
= &ms_softc
[minor(dev
)];
418 return (ev_kqfilter(&ms
->ms_events
, kn
));
420 #endif /* NMOUSE > 0 */