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