xref: /netbsd-src/sys/arch/atari/dev/ms.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: ms.c,v 1.26 2014/07/25 08:10:32 dholland 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.26 2014/07/25 08:10:32 dholland 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)(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 	.d_open = msopen,
95 	.d_close = msclose,
96 	.d_read = msread,
97 	.d_write = nowrite,
98 	.d_ioctl = msioctl,
99 	.d_stop = nostop,
100 	.d_tty = notty,
101 	.d_poll = mspoll,
102 	.d_mmap = nommap,
103 	.d_kqfilter = mskqfilter,
104 	.d_discard = nodiscard,
105 	.d_flag = 0
106 };
107 
108 static	void	ms_3b_delay(struct ms_softc *);
109 
110 int
111 mouseattach(int cnt)
112 {
113 	printf("1 mouse configured\n");
114 	ms_softc[0].ms_emul3b = 1;
115 	callout_init(&ms_softc[0].ms_delay_ch, 0);
116 	return(NMOUSE);
117 }
118 
119 static void
120 ms_3b_delay(struct ms_softc *ms)
121 {
122 	REL_MOUSE	rel_ms;
123 
124 	rel_ms.id = TIMEOUT_ID;
125 	rel_ms.dx = rel_ms.dy = 0;
126 	mouse_soft(&rel_ms, sizeof(rel_ms), KBD_TIMEO_PKG);
127 }
128 /*
129  * Note that we are called from the keyboard software interrupt!
130  */
131 void
132 mouse_soft(REL_MOUSE *rel_ms, int size, int type)
133 {
134 	struct ms_softc		*ms = &ms_softc[0];
135 	struct firm_event	*fe, *fe2;
136 	REL_MOUSE		fake_mouse;
137 	int			get, put;
138 	int			sps;
139 	u_char			mbut, bmask;
140 	int			flush_buttons;
141 
142 	if (ms->ms_events.ev_io == NULL)
143 		return;
144 
145 	switch (type) {
146 	    case KBD_JOY1_PKG:
147 		/*
148 		 * Ignore if in emulation mode
149 		 */
150 		if (ms->ms_emul3b)
151 			return;
152 
153 		/*
154 		 * There are some mice that have their middle button
155 		 * wired to the 'up' bit of joystick 1....
156 		 * Simulate a mouse packet with dx = dy = 0, the middle
157 		 * button state set by UP and the other buttons unchanged.
158 		 * Flush all button changes.
159 		 */
160 		flush_buttons = 1;
161 		fake_mouse.id = (rel_ms->dx & 1 ? 4 : 0) | (ms->ms_buttons & 3);
162 		fake_mouse.dx = fake_mouse.dy = 0;
163 		rel_ms = &fake_mouse;
164 		break;
165 	    case KBD_TIMEO_PKG:
166 		/*
167 		 * Timeout package. No button changes and no movement.
168 		 * Flush all button changes.
169 		 */
170 		flush_buttons = 1;
171 		fake_mouse.id = ms->ms_buttons;
172 		fake_mouse.dx = fake_mouse.dy = 0;
173 		rel_ms = &fake_mouse;
174 		break;
175 	    case KBD_RMS_PKG:
176 		/*
177 		 * Normal mouse package. Always copy the middle button
178 		 * status. The emulation code decides if button changes
179 		 * must be flushed.
180 		 */
181 		rel_ms->id = (ms->ms_buttons & 4) | (rel_ms->id & 3);
182 		flush_buttons = (ms->ms_emul3b) ? 0 : 1;
183 		break;
184 	    default:
185 		return;
186 	}
187 
188 	sps = splev();
189 	get = ms->ms_events.ev_get;
190 	put = ms->ms_events.ev_put;
191 	fe  = &ms->ms_events.ev_q[put];
192 
193 	if ((type != KBD_TIMEO_PKG) && ms->ms_emul3b && ms->ms_bq_idx)
194 		callout_stop(&ms->ms_delay_ch);
195 
196 	/*
197 	 * Button states are encoded in the lower 3 bits of 'id'
198 	 */
199 	if (!(mbut = (rel_ms->id ^ ms->ms_buttons)) && (put != get)) {
200 		/*
201 		 * Compact dx/dy messages. Always generate an event when
202 		 * a button is pressed or the event queue is empty.
203 		 */
204 		ms->ms_dx += rel_ms->dx;
205 		ms->ms_dy += rel_ms->dy;
206 		goto out;
207 	}
208 	rel_ms->dx += ms->ms_dx;
209 	rel_ms->dy += ms->ms_dy;
210 	ms->ms_dx = ms->ms_dy = 0;
211 
212 	/*
213 	 * Output location events _before_ button events ie. make sure
214 	 * the button is pressed at the correct location.
215 	 */
216 	if (rel_ms->dx) {
217 		if ((++put) % EV_QSIZE == get) {
218 			put--;
219 			goto out;
220 		}
221 		fe->id    = LOC_X_DELTA;
222 		fe->value = rel_ms->dx;
223 		firm_gettime(fe);
224 		if (put >= EV_QSIZE) {
225 			put = 0;
226 			fe  = &ms->ms_events.ev_q[0];
227 		}
228 		else fe++;
229 	}
230 	if (rel_ms->dy) {
231 		if ((++put) % EV_QSIZE == get) {
232 			put--;
233 			goto out;
234 		}
235 		fe->id    = LOC_Y_DELTA;
236 		fe->value = rel_ms->dy;
237 		firm_gettime(fe);
238 		if (put >= EV_QSIZE) {
239 			put = 0;
240 			fe  = &ms->ms_events.ev_q[0];
241 		}
242 		else fe++;
243 	}
244 	if (mbut && (type != KBD_TIMEO_PKG)) {
245 		for (bmask = 1; bmask < 0x08; bmask <<= 1) {
246 			if (!(mbut & bmask))
247 				continue;
248 			fe2 = &ms->ms_bq[ms->ms_bq_idx++];
249 			if (bmask == 1)
250 				fe2->id = MS_RIGHT;
251 			else if (bmask == 2)
252 				fe2->id = MS_LEFT;
253 			else fe2->id = MS_MIDDLE;
254 			fe2->value = rel_ms->id & bmask ? VKEY_DOWN : VKEY_UP;
255 			firm_gettime(fe2);
256 		}
257 	}
258 
259 	/*
260 	 * Handle 3rd button emulation.
261 	 */
262 	if (ms->ms_emul3b && ms->ms_bq_idx && (type != KBD_TIMEO_PKG)) {
263 		/*
264 		 * If the middle button is pressed, any change to
265 		 * one of the other buttons releases all.
266 		 */
267 		if ((ms->ms_buttons & 4) && (mbut & 3)) {
268 			ms->ms_bq[0].id = MS_MIDDLE;
269 			ms->ms_bq_idx   = 1;
270 			rel_ms->id      = 0;
271 			flush_buttons   = 1;
272 			goto out;
273 		}
274 	    	if (ms->ms_bq_idx == 2) {
275 			if (ms->ms_bq[0].value == ms->ms_bq[1].value) {
276 				/* Must be 2 button presses! */
277 				ms->ms_bq[0].id = MS_MIDDLE;
278 				ms->ms_bq_idx   = 1;
279 				rel_ms->id      = 7;
280 			}
281 		}
282 		else if (ms->ms_bq[0].value == VKEY_DOWN) {
283 			callout_reset(&ms->ms_delay_ch, 10,
284 			    (FPV)ms_3b_delay, (void *)ms);
285 			goto out;
286 		}
287 		flush_buttons   = 1;
288 	}
289 out:
290 	if (flush_buttons) {
291 		int	i;
292 
293 		for (i = 0; i < ms->ms_bq_idx; i++) {
294 			if ((++put) % EV_QSIZE == get) {
295 				ms->ms_bq_idx = 0;
296 				put--;
297 				goto out;
298 			}
299 			*fe = ms->ms_bq[i];
300 			if (put >= EV_QSIZE) {
301 				put = 0;
302 				fe  = &ms->ms_events.ev_q[0];
303 			}
304 			else fe++;
305 		}
306 		ms->ms_bq_idx = 0;
307 	}
308 	ms->ms_events.ev_put = put;
309 	ms->ms_buttons       = rel_ms->id;
310 	splx(sps);
311 	EV_WAKEUP(&ms->ms_events);
312 }
313 
314 int
315 msopen(dev_t dev, int flags, int mode, struct lwp *l)
316 {
317 	u_char		report_ms_joy[] = { 0x14, 0x08 };
318 	struct ms_softc	*ms;
319 	int		unit;
320 
321 	unit = minor(dev);
322 	ms   = &ms_softc[unit];
323 
324 	if (unit >= NMOUSE)
325 		return(EXDEV);
326 
327 	if (ms->ms_events.ev_io)
328 		return(EBUSY);
329 
330 	ms->ms_events.ev_io = l->l_proc;
331 	ms->ms_dx = ms->ms_dy = 0;
332 	ms->ms_buttons = 0;
333 	ms->ms_bq[0].id = ms->ms_bq[1].id = 0;
334 	ms->ms_bq_idx = 0;
335 	ev_init(&ms->ms_events);	/* may cause sleep */
336 
337 	/*
338 	 * Enable mouse reporting.
339 	 */
340 	kbd_write(report_ms_joy, sizeof(report_ms_joy));
341 	return(0);
342 }
343 
344 int
345 msclose(dev_t dev, int flags, int mode, struct lwp *l)
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_t dev, struct uio *uio, int flags)
365 {
366 	struct ms_softc *ms;
367 
368 	ms = &ms_softc[minor(dev)];
369 	return(ev_read(&ms->ms_events, uio, flags));
370 }
371 
372 int
373 msioctl(dev_t dev, u_long cmd, register void * data, int flag, struct lwp *l)
374 {
375 	struct ms_softc *ms;
376 	int		unit;
377 
378 	unit = minor(dev);
379 	ms = &ms_softc[unit];
380 
381 	switch (cmd) {
382 	case  MIOCS3B_EMUL:
383 		ms->ms_emul3b = (*(int *)data != 0) ? 1 : 0;
384 		return (0);
385 	case  MIOCG3B_EMUL:
386 		*(int *)data = ms->ms_emul3b;
387 		return (0);
388 	case FIONBIO:		/* we will remove this someday (soon???) */
389 		return(0);
390 	case FIOASYNC:
391 		ms->ms_events.ev_async = *(int *)data != 0;
392 		return(0);
393 	case FIOSETOWN:
394 		if (-*(int *)data != ms->ms_events.ev_io->p_pgid
395 		    && *(int *)data != ms->ms_events.ev_io->p_pid)
396 			return(EPERM);
397 		return(0);
398 	case TIOCSPGRP:
399 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
400 			return(EPERM);
401 		return(0);
402 	case VUIDGFORMAT:	/* we only do firm_events */
403 		*(int *)data = VUID_FIRM_EVENT;
404 		return(0);
405 	case VUIDSFORMAT:
406 		if (*(int *)data != VUID_FIRM_EVENT)
407 			return(EINVAL);
408 		return(0);
409 	}
410 	return(ENOTTY);
411 }
412 
413 int
414 mspoll(dev_t dev, int events, struct lwp *l)
415 {
416 	struct ms_softc *ms;
417 
418 	ms = &ms_softc[minor(dev)];
419 	return(ev_poll(&ms->ms_events, events, l));
420 }
421 
422 int
423 mskqfilter(dev_t dev, struct knote *kn)
424 {
425 	struct ms_softc *ms;
426 
427 	ms = &ms_softc[minor(dev)];
428 	return (ev_kqfilter(&ms->ms_events, kn));
429 }
430 #endif /* NMOUSE > 0 */
431