xref: /netbsd-src/sys/arch/atari/dev/ms.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: ms.c,v 1.15 2003/09/21 19:16:52 jdolecek Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman.
5  * All rights reserved.
6  *
7  * based on:
8  *
9  * Copyright (c) 1992, 1993
10  *	The Regents of the University of California.  All rights reserved.
11  *
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.
15  *
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.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
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.
32  *
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
43  * SUCH DAMAGE.
44  *
45  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
46  *
47  * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp  (LBL)
48  */
49 
50 /*
51  * Mouse driver.
52  */
53 
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.15 2003/09/21 19:16:52 jdolecek Exp $");
56 
57 #include <sys/param.h>
58 #include <sys/conf.h>
59 #include <sys/ioctl.h>
60 #include <sys/kernel.h>
61 #include <sys/proc.h>
62 #include <sys/systm.h>
63 #include <sys/callout.h>
64 #include <sys/tty.h>
65 #include <sys/signalvar.h>
66 
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>
72 
73 #include "mouse.h"
74 #if NMOUSE > 0
75 
76 /* there's really no more physical ports on an atari. */
77 #if NMOUSE > 1
78 #undef NMOUSE
79 #define NMOUSE 1
80 #endif
81 
82 typedef void	(*FPV) __P((void *));
83 
84 static struct ms_softc	ms_softc[NMOUSE];
85 
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);
92 
93 const struct cdevsw ms_cdevsw = {
94 	msopen, msclose, msread, nowrite, msioctl,
95 	nostop, notty, mspoll, nommap, mskqfilter,
96 };
97 
98 static	void	ms_3b_delay __P((struct ms_softc *));
99 
100 int
101 mouseattach(cnt)
102 	int cnt;
103 {
104 	printf("1 mouse configured\n");
105 	ms_softc[0].ms_emul3b = 1;
106 	callout_init(&ms_softc[0].ms_delay_ch);
107 	return(NMOUSE);
108 }
109 
110 static void
111 ms_3b_delay(ms)
112 struct ms_softc	*ms;
113 {
114 	REL_MOUSE	rel_ms;
115 
116 	rel_ms.id = TIMEOUT_ID;
117 	rel_ms.dx = rel_ms.dy = 0;
118 	mouse_soft(&rel_ms, sizeof(rel_ms), KBD_TIMEO_PKG);
119 }
120 /*
121  * Note that we are called from the keyboard software interrupt!
122  */
123 void
124 mouse_soft(rel_ms, size, type)
125 REL_MOUSE	*rel_ms;
126 int		size, type;
127 {
128 	struct ms_softc		*ms = &ms_softc[0];
129 	struct firm_event	*fe, *fe2;
130 	REL_MOUSE		fake_mouse;
131 	int			get, put;
132 	int			sps;
133 	u_char			mbut, bmask;
134 	int			flush_buttons;
135 
136 	if (ms->ms_events.ev_io == NULL)
137 		return;
138 
139 	switch (type) {
140 	    case KBD_JOY1_PKG:
141 		/*
142 		 * Ignore if in emulation mode
143 		 */
144 		if (ms->ms_emul3b)
145 			return;
146 
147 		/*
148 		 * There are some mice that have their middle button
149 		 * wired to the 'up' bit of joystick 1....
150 		 * Simulate a mouse packet with dx = dy = 0, the middle
151 		 * button state set by UP and the other buttons unchanged.
152 		 * Flush all button changes.
153 		 */
154 		flush_buttons = 1;
155 		fake_mouse.id = (rel_ms->dx & 1 ? 4 : 0) | (ms->ms_buttons & 3);
156 		fake_mouse.dx = fake_mouse.dy = 0;
157 		rel_ms = &fake_mouse;
158 		break;
159 	    case KBD_TIMEO_PKG:
160 		/*
161 		 * Timeout package. No button changes and no movement.
162 		 * Flush all button changes.
163 		 */
164 		flush_buttons = 1;
165 		fake_mouse.id = ms->ms_buttons;
166 		fake_mouse.dx = fake_mouse.dy = 0;
167 		rel_ms = &fake_mouse;
168 		break;
169 	    case KBD_RMS_PKG:
170 		/*
171 		 * Normal mouse package. Always copy the middle button
172 		 * status. The emulation code decides if button changes
173 		 * must be flushed.
174 		 */
175 		rel_ms->id = (ms->ms_buttons & 4) | (rel_ms->id & 3);
176 		flush_buttons = (ms->ms_emul3b) ? 0 : 1;
177 		break;
178 	    default:
179 		return;
180 	}
181 
182 	sps = splev();
183 	get = ms->ms_events.ev_get;
184 	put = ms->ms_events.ev_put;
185 	fe  = &ms->ms_events.ev_q[put];
186 
187 	if ((type != KBD_TIMEO_PKG) && ms->ms_emul3b && ms->ms_bq_idx)
188 		callout_stop(&ms->ms_delay_ch);
189 
190 	/*
191 	 * Button states are encoded in the lower 3 bits of 'id'
192 	 */
193 	if (!(mbut = (rel_ms->id ^ ms->ms_buttons)) && (put != get)) {
194 		/*
195 		 * Compact dx/dy messages. Always generate an event when
196 		 * a button is pressed or the event queue is empty.
197 		 */
198 		ms->ms_dx += rel_ms->dx;
199 		ms->ms_dy += rel_ms->dy;
200 		goto out;
201 	}
202 	rel_ms->dx += ms->ms_dx;
203 	rel_ms->dy += ms->ms_dy;
204 	ms->ms_dx = ms->ms_dy = 0;
205 
206 	/*
207 	 * Output location events _before_ button events ie. make sure
208 	 * the button is pressed at the correct location.
209 	 */
210 	if (rel_ms->dx) {
211 		if ((++put) % EV_QSIZE == get) {
212 			put--;
213 			goto out;
214 		}
215 		fe->id    = LOC_X_DELTA;
216 		fe->value = rel_ms->dx;
217 		fe->time  = time;
218 		if (put >= EV_QSIZE) {
219 			put = 0;
220 			fe  = &ms->ms_events.ev_q[0];
221 		}
222 		else fe++;
223 	}
224 	if (rel_ms->dy) {
225 		if ((++put) % EV_QSIZE == get) {
226 			put--;
227 			goto out;
228 		}
229 		fe->id    = LOC_Y_DELTA;
230 		fe->value = rel_ms->dy;
231 		fe->time  = time;
232 		if (put >= EV_QSIZE) {
233 			put = 0;
234 			fe  = &ms->ms_events.ev_q[0];
235 		}
236 		else fe++;
237 	}
238 	if (mbut && (type != KBD_TIMEO_PKG)) {
239 		for (bmask = 1; bmask < 0x08; bmask <<= 1) {
240 			if (!(mbut & bmask))
241 				continue;
242 			fe2 = &ms->ms_bq[ms->ms_bq_idx++];
243 			if (bmask == 1)
244 				fe2->id = MS_RIGHT;
245 			else if (bmask == 2)
246 				fe2->id = MS_LEFT;
247 			else fe2->id = MS_MIDDLE;
248 			fe2->value = rel_ms->id & bmask ? VKEY_DOWN : VKEY_UP;
249 			fe2->time  = time;
250 		}
251 	}
252 
253 	/*
254 	 * Handle 3rd button emulation.
255 	 */
256 	if (ms->ms_emul3b && ms->ms_bq_idx && (type != KBD_TIMEO_PKG)) {
257 		/*
258 		 * If the middle button is pressed, any change to
259 		 * one of the other buttons releases all.
260 		 */
261 		if ((ms->ms_buttons & 4) && (mbut & 3)) {
262 			ms->ms_bq[0].id = MS_MIDDLE;
263 			ms->ms_bq_idx   = 1;
264 			rel_ms->id      = 0;
265 			flush_buttons   = 1;
266 			goto out;
267 		}
268 	    	if (ms->ms_bq_idx == 2) {
269 			if (ms->ms_bq[0].value == ms->ms_bq[1].value) {
270 				/* Must be 2 button presses! */
271 				ms->ms_bq[0].id = MS_MIDDLE;
272 				ms->ms_bq_idx   = 1;
273 				rel_ms->id      = 7;
274 			}
275 		}
276 		else if (ms->ms_bq[0].value == VKEY_DOWN) {
277 			callout_reset(&ms->ms_delay_ch, 10,
278 			    (FPV)ms_3b_delay, (void *)ms);
279 			goto out;
280 		}
281 		flush_buttons   = 1;
282 	}
283 out:
284 	if (flush_buttons) {
285 		int	i;
286 
287 		for (i = 0; i < ms->ms_bq_idx; i++) {
288 			if ((++put) % EV_QSIZE == get) {
289 				ms->ms_bq_idx = 0;
290 				put--;
291 				goto out;
292 			}
293 			*fe = ms->ms_bq[i];
294 			if (put >= EV_QSIZE) {
295 				put = 0;
296 				fe  = &ms->ms_events.ev_q[0];
297 			}
298 			else fe++;
299 		}
300 		ms->ms_bq_idx = 0;
301 	}
302 	ms->ms_events.ev_put = put;
303 	ms->ms_buttons       = rel_ms->id;
304 	splx(sps);
305 	EV_WAKEUP(&ms->ms_events);
306 }
307 
308 int
309 msopen(dev, flags, mode, p)
310 dev_t		dev;
311 int		flags, mode;
312 struct proc	*p;
313 {
314 	u_char		report_ms_joy[] = { 0x14, 0x08 };
315 	struct ms_softc	*ms;
316 	int		unit;
317 
318 	unit = minor(dev);
319 	ms   = &ms_softc[unit];
320 
321 	if (unit >= NMOUSE)
322 		return(EXDEV);
323 
324 	if (ms->ms_events.ev_io)
325 		return(EBUSY);
326 
327 	ms->ms_events.ev_io = p;
328 	ms->ms_dx = ms->ms_dy = 0;
329 	ms->ms_buttons = 0;
330 	ms->ms_bq[0].id = ms->ms_bq[1].id = 0;
331 	ms->ms_bq_idx = 0;
332 	ev_init(&ms->ms_events);	/* may cause sleep */
333 
334 	/*
335 	 * Enable mouse reporting.
336 	 */
337 	kbd_write(report_ms_joy, sizeof(report_ms_joy));
338 	return(0);
339 }
340 
341 int
342 msclose(dev, flags, mode, p)
343 dev_t		dev;
344 int		flags, mode;
345 struct proc	*p;
346 {
347 	u_char		disable_ms_joy[] = { 0x12, 0x1a };
348 	int		unit;
349 	struct ms_softc	*ms;
350 
351 	unit = minor (dev);
352 	ms   = &ms_softc[unit];
353 
354 	/*
355 	 * Turn off mouse interrogation.
356 	 */
357 	kbd_write(disable_ms_joy, sizeof(disable_ms_joy));
358 	ev_fini(&ms->ms_events);
359 	ms->ms_events.ev_io = NULL;
360 	return(0);
361 }
362 
363 int
364 msread(dev, uio, flags)
365 dev_t		dev;
366 struct uio	*uio;
367 int		flags;
368 {
369 	struct ms_softc *ms;
370 
371 	ms = &ms_softc[minor(dev)];
372 	return(ev_read(&ms->ms_events, uio, flags));
373 }
374 
375 int
376 msioctl(dev, cmd, data, flag, p)
377 dev_t			dev;
378 u_long			cmd;
379 register caddr_t 	data;
380 int			flag;
381 struct proc		*p;
382 {
383 	struct ms_softc *ms;
384 	int		unit;
385 
386 	unit = minor(dev);
387 	ms = &ms_softc[unit];
388 
389 	switch (cmd) {
390 	case  MIOCS3B_EMUL:
391 		ms->ms_emul3b = (*(int *)data != 0) ? 1 : 0;
392 		return (0);
393 	case  MIOCG3B_EMUL:
394 		*(int *)data = ms->ms_emul3b;
395 		return (0);
396 	case FIONBIO:		/* we will remove this someday (soon???) */
397 		return(0);
398 	case FIOASYNC:
399 		ms->ms_events.ev_async = *(int *)data != 0;
400 		return(0);
401 	case FIOSETOWN:
402 		if (-*(int *)data != ms->ms_events.ev_io->p_pgid
403 		    && *(int *)data != ms->ms_events.ev_io->p_pid)
404 			return(EPERM);
405 		return(0);
406 	case TIOCSPGRP:
407 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
408 			return(EPERM);
409 		return(0);
410 	case VUIDGFORMAT:	/* we only do firm_events */
411 		*(int *)data = VUID_FIRM_EVENT;
412 		return(0);
413 	case VUIDSFORMAT:
414 		if (*(int *)data != VUID_FIRM_EVENT)
415 			return(EINVAL);
416 		return(0);
417 	}
418 	return(ENOTTY);
419 }
420 
421 int
422 mspoll(dev, events, p)
423 dev_t		dev;
424 int		events;
425 struct proc	*p;
426 {
427 	struct ms_softc *ms;
428 
429 	ms = &ms_softc[minor(dev)];
430 	return(ev_poll(&ms->ms_events, events, p));
431 }
432 
433 int
434 mskqfilter(dev_t dev, struct knote *kn)
435 {
436 	struct ms_softc *ms;
437 
438 	ms = &ms_softc[minor(dev)];
439 	return (ev_kqfilter(&ms->ms_events, kn));
440 }
441 #endif /* NMOUSE > 0 */
442