ms.c revision 1.3 1 /* $NetBSD: ms.c,v 1.3 1995/07/27 06:35:46 leo 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/param.h>
59 #include <sys/conf.h>
60 #include <sys/ioctl.h>
61 #include <sys/kernel.h>
62 #include <sys/proc.h>
63 #include <sys/systm.h>
64 #include <sys/tty.h>
65
66 #include <atari/dev/event_var.h>
67 #include <atari/dev/vuid_event.h>
68
69 #include "mouse.h"
70 #if NMOUSE > 0
71
72 /* there's really no more physical ports on an atari. */
73 #if NMOUSE > 1
74 #undef NMOUSE
75 #define NMOUSE 1
76 #endif
77
78 typedef void (*FPV)();
79
80 /*
81 * Mouse specific packages produced by the keyboard. Currently, we only
82 * define the REL_MOUSE package, as this is the only one used.
83 */
84 typedef struct {
85 u_char id;
86 char dx;
87 char dy;
88 } REL_MOUSE;
89
90 #define IS_REL_MOUSE(id) (((u_int)(id) & 0xF8) == 0xF8)
91 #define TIMEOUT_ID (0xFC)
92
93 static struct ms_softc {
94 u_char ms_buttons; /* button states */
95 struct evvar ms_events; /* event queue state */
96 int ms_dx; /* accumulated dx */
97 int ms_dy; /* accumulated dy */
98 struct firm_event ms_bq[2]; /* Button queue */
99 int ms_bq_idx; /* Button queue index */
100 } ms_softc[NMOUSE];
101
102 static void ms_3b_delay __P((struct ms_softc *));
103 void mouse_soft __P((REL_MOUSE *, int));
104
105 int
106 mouseattach(cnt)
107 int cnt;
108 {
109 printf("1 mouse configured\n");
110 return(NMOUSE);
111 }
112
113 static void
114 ms_3b_delay(ms)
115 struct ms_softc *ms;
116 {
117 REL_MOUSE rel_ms;
118
119 rel_ms.id = TIMEOUT_ID;
120 rel_ms.dx = rel_ms.dy = 0;
121 mouse_soft(&rel_ms, sizeof(rel_ms));
122 }
123 /*
124 * Note that we are called from the keyboard software interrupt!
125 */
126 void
127 mouse_soft(rel_ms, size)
128 REL_MOUSE *rel_ms;
129 int size;
130 {
131 struct ms_softc *ms = &ms_softc[0];
132 struct firm_event *fe, *fe2;
133 int get, put;
134 int sps;
135 u_char mbut, bmask;
136 int is_timeout;
137 int flush_buttons;
138 int id;
139
140 if (!IS_REL_MOUSE(rel_ms->id))
141 return; /* Probably some other message */
142 if (ms->ms_events.ev_io == NULL)
143 return;
144
145 sps = splev();
146 get = ms->ms_events.ev_get;
147 put = ms->ms_events.ev_put;
148 fe = &ms->ms_events.ev_q[put];
149
150 if (rel_ms->id == TIMEOUT_ID) {
151 is_timeout = 1;
152 id = ms->ms_buttons;
153 }
154 else {
155 is_timeout = 0;
156 id = (rel_ms->id & 3) | (ms->ms_buttons & 4);
157 }
158
159 if (!is_timeout && ms->ms_bq_idx)
160 untimeout((FPV)ms_3b_delay, (void *)ms);
161
162 /*
163 * Button states are encoded in the lower 2 bits of 'id'
164 */
165 if (!(mbut = (id ^ ms->ms_buttons)) && (put != get)) {
166 /*
167 * Compact dx/dy messages. Always generate an event when
168 * a button is pressed or the event queue is empty.
169 */
170 ms->ms_dx += rel_ms->dx;
171 ms->ms_dy += rel_ms->dy;
172 goto out;
173 }
174 rel_ms->dx += ms->ms_dx;
175 rel_ms->dy += ms->ms_dy;
176 ms->ms_dx = ms->ms_dy = 0;
177
178 /*
179 * Output location events _before_ button events ie. make sure
180 * the button is pressed at the correct location.
181 */
182 if (rel_ms->dx) {
183 if ((++put) % EV_QSIZE == get) {
184 put--;
185 goto out;
186 }
187 fe->id = LOC_X_DELTA;
188 fe->value = rel_ms->dx;
189 fe->time = time;
190 if (put >= EV_QSIZE) {
191 put = 0;
192 fe = &ms->ms_events.ev_q[0];
193 }
194 else fe++;
195 }
196 if (rel_ms->dy) {
197 if ((++put) % EV_QSIZE == get) {
198 put--;
199 goto out;
200 }
201 fe->id = LOC_Y_DELTA;
202 fe->value = rel_ms->dy;
203 fe->time = time;
204 if (put >= EV_QSIZE) {
205 put = 0;
206 fe = &ms->ms_events.ev_q[0];
207 }
208 else fe++;
209 }
210 if (mbut && !is_timeout) {
211 for (bmask = 1; bmask < 0x04; bmask <<= 1) {
212 if (!(mbut & bmask))
213 continue;
214 fe2 = &ms->ms_bq[ms->ms_bq_idx++];
215 fe2->id = bmask & 1 ? MS_RIGHT : MS_LEFT;
216 fe2->value = id & bmask ? VKEY_DOWN : VKEY_UP;
217 fe2->time = time;
218 }
219 }
220 if (ms->ms_bq_idx) {
221 /*
222 * We have at least one button, handle it.
223 */
224 flush_buttons = (is_timeout) ? 1 : 0;
225 if (ms->ms_bq_idx == 2) {
226 if (ms->ms_bq[0].value == ms->ms_bq[1].value) {
227 /* Must be 2 button presses! */
228 if (ms->ms_bq[0].id != ms->ms_bq[1].id) {
229 ms->ms_bq[0].id = MS_MIDDLE;
230 ms->ms_bq_idx = 1;
231 id = 7;
232 }
233 }
234 flush_buttons = 1;
235 }
236 else {
237 if (ms->ms_bq[0].value == VKEY_UP) {
238 /*
239 * Release of a button is always flushed
240 * immediately. If the middle button is
241 * active, the release event is his. Mark
242 * all buttons released, this also surpresses
243 * a spurious release event of the not-yet-
244 * released button.
245 */
246 if( id & 4) {
247 ms->ms_bq[0].id = MS_MIDDLE;
248 id = 0;
249 }
250 flush_buttons = 1;
251 }
252 else if (!is_timeout) {
253 timeout((FPV)ms_3b_delay, (void *)ms, 10);
254 goto out;
255 }
256 }
257 if (flush_buttons) {
258 int i;
259
260 for (i = 0; i < ms->ms_bq_idx; i++) {
261 if ((++put) % EV_QSIZE == get) {
262 ms->ms_bq_idx = 0;
263 put--;
264 goto out;
265 }
266 *fe = ms->ms_bq[i];
267 if (put >= EV_QSIZE) {
268 put = 0;
269 fe = &ms->ms_events.ev_q[0];
270 }
271 else fe++;
272 }
273 ms->ms_bq_idx = 0;
274 }
275 }
276
277 out:
278 ms->ms_events.ev_put = put;
279 ms->ms_buttons = id;
280 splx(sps);
281 EV_WAKEUP(&ms->ms_events);
282 }
283
284 int
285 msopen(dev, flags, mode, p)
286 dev_t dev;
287 int flags, mode;
288 struct proc *p;
289 {
290 u_char report_ms[] = { 0x08 };
291 struct ms_softc *ms;
292 int unit;
293
294 unit = minor(dev);
295 ms = &ms_softc[unit];
296
297 if (unit >= NMOUSE)
298 return(EXDEV);
299
300 if (ms->ms_events.ev_io)
301 return(EBUSY);
302
303 ms->ms_events.ev_io = p;
304 ms->ms_dx = ms->ms_dy = 0;
305 ms->ms_buttons = 0;
306 ms->ms_bq[0].id = ms->ms_bq[1].id = 0;
307 ms->ms_bq_idx = 0;
308 ev_init(&ms->ms_events); /* may cause sleep */
309
310 /*
311 * Enable mouse reporting.
312 */
313 kbd_write(report_ms, sizeof(report_ms));
314 return(0);
315 }
316
317 int
318 msclose(dev, flags, mode, p)
319 dev_t dev;
320 int flags, mode;
321 struct proc *p;
322 {
323 u_char disable_ms[] = { 0x12 };
324 int unit;
325 struct ms_softc *ms;
326
327 unit = minor (dev);
328 ms = &ms_softc[unit];
329
330 /*
331 * Turn off mouse interrogation.
332 */
333 kbd_write(disable_ms, sizeof(disable_ms));
334 ev_fini(&ms->ms_events);
335 ms->ms_events.ev_io = NULL;
336 return(0);
337 }
338
339 int
340 msread(dev, uio, flags)
341 dev_t dev;
342 struct uio *uio;
343 int flags;
344 {
345 struct ms_softc *ms;
346
347 ms = &ms_softc[minor(dev)];
348 return(ev_read(&ms->ms_events, uio, flags));
349 }
350
351 int
352 msioctl(dev, cmd, data, flag, p)
353 dev_t dev;
354 u_long cmd;
355 register caddr_t data;
356 int flag;
357 struct proc *p;
358 {
359 struct ms_softc *ms;
360 int unit;
361
362 unit = minor(dev);
363 ms = &ms_softc[unit];
364
365 switch (cmd) {
366 case FIONBIO: /* we will remove this someday (soon???) */
367 return(0);
368 case FIOASYNC:
369 ms->ms_events.ev_async = *(int *)data != 0;
370 return(0);
371 case TIOCSPGRP:
372 if (*(int *)data != ms->ms_events.ev_io->p_pgid)
373 return(EPERM);
374 return(0);
375 case VUIDGFORMAT: /* we only do firm_events */
376 *(int *)data = VUID_FIRM_EVENT;
377 return(0);
378 case VUIDSFORMAT:
379 if (*(int *)data != VUID_FIRM_EVENT)
380 return(EINVAL);
381 return(0);
382 }
383 return(ENOTTY);
384 }
385
386 int
387 msselect(dev, rw, p)
388 dev_t dev;
389 int rw;
390 struct proc *p;
391 {
392 struct ms_softc *ms;
393
394 ms = &ms_softc[minor(dev)];
395 return(ev_select(&ms->ms_events, rw, p));
396 }
397 #endif /* NMOUSE > 0 */
398