ms.c revision 1.6 1 /* $NetBSD: ms.c,v 1.6 1996/04/23 11:02:01 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 #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_select(msselect);
91
92 static void ms_3b_delay __P((struct ms_softc *));
93
94 int
95 mouseattach(cnt)
96 int cnt;
97 {
98 printf("1 mouse configured\n");
99 ms_softc[0].ms_emul3b = 1;
100 return(NMOUSE);
101 }
102
103 static void
104 ms_3b_delay(ms)
105 struct ms_softc *ms;
106 {
107 REL_MOUSE rel_ms;
108
109 rel_ms.id = TIMEOUT_ID;
110 rel_ms.dx = rel_ms.dy = 0;
111 mouse_soft(&rel_ms, sizeof(rel_ms), KBD_TIMEO_PKG);
112 }
113 /*
114 * Note that we are called from the keyboard software interrupt!
115 */
116 void
117 mouse_soft(rel_ms, size, type)
118 REL_MOUSE *rel_ms;
119 int size, type;
120 {
121 struct ms_softc *ms = &ms_softc[0];
122 struct firm_event *fe, *fe2;
123 REL_MOUSE fake_mouse;
124 int get, put;
125 int sps;
126 u_char mbut, bmask;
127 int flush_buttons;
128
129 if (ms->ms_events.ev_io == NULL)
130 return;
131
132 switch (type) {
133 case KBD_JOY1_PKG:
134 /*
135 * Ignore if in emulation mode
136 */
137 if (ms->ms_emul3b)
138 return;
139
140 /*
141 * There are some mice that have their middle button
142 * wired to the 'up' bit of joystick 1....
143 * Simulate a mouse packet with dx = dy = 0, the middle
144 * button state set by UP and the other buttons unchanged.
145 * Flush all button changes.
146 */
147 flush_buttons = 1;
148 fake_mouse.id = (rel_ms->dx & 1 ? 4 : 0) | (ms->ms_buttons & 3);
149 fake_mouse.dx = fake_mouse.dy = 0;
150 rel_ms = &fake_mouse;
151 break;
152 case KBD_TIMEO_PKG:
153 /*
154 * Timeout package. No button changes and no movement.
155 * Flush all button changes.
156 */
157 flush_buttons = 1;
158 fake_mouse.id = ms->ms_buttons;
159 fake_mouse.dx = fake_mouse.dy = 0;
160 rel_ms = &fake_mouse;
161 break;
162 case KBD_RMS_PKG:
163 /*
164 * Normal mouse package. Always copy the middle button
165 * status. The emulation code decides if button changes
166 * must be flushed.
167 */
168 rel_ms->id = (ms->ms_buttons & 4) | (rel_ms->id & 3);
169 flush_buttons = (ms->ms_emul3b) ? 0 : 1;
170 break;
171 default:
172 return;
173 }
174
175 sps = splev();
176 get = ms->ms_events.ev_get;
177 put = ms->ms_events.ev_put;
178 fe = &ms->ms_events.ev_q[put];
179
180 if ((type != KBD_TIMEO_PKG) && ms->ms_emul3b && ms->ms_bq_idx)
181 untimeout((FPV)ms_3b_delay, (void *)ms);
182
183 /*
184 * Button states are encoded in the lower 3 bits of 'id'
185 */
186 if (!(mbut = (rel_ms->id ^ ms->ms_buttons)) && (put != get)) {
187 /*
188 * Compact dx/dy messages. Always generate an event when
189 * a button is pressed or the event queue is empty.
190 */
191 ms->ms_dx += rel_ms->dx;
192 ms->ms_dy += rel_ms->dy;
193 goto out;
194 }
195 rel_ms->dx += ms->ms_dx;
196 rel_ms->dy += ms->ms_dy;
197 ms->ms_dx = ms->ms_dy = 0;
198
199 /*
200 * Output location events _before_ button events ie. make sure
201 * the button is pressed at the correct location.
202 */
203 if (rel_ms->dx) {
204 if ((++put) % EV_QSIZE == get) {
205 put--;
206 goto out;
207 }
208 fe->id = LOC_X_DELTA;
209 fe->value = rel_ms->dx;
210 fe->time = time;
211 if (put >= EV_QSIZE) {
212 put = 0;
213 fe = &ms->ms_events.ev_q[0];
214 }
215 else fe++;
216 }
217 if (rel_ms->dy) {
218 if ((++put) % EV_QSIZE == get) {
219 put--;
220 goto out;
221 }
222 fe->id = LOC_Y_DELTA;
223 fe->value = rel_ms->dy;
224 fe->time = time;
225 if (put >= EV_QSIZE) {
226 put = 0;
227 fe = &ms->ms_events.ev_q[0];
228 }
229 else fe++;
230 }
231 if (mbut && (type != KBD_TIMEO_PKG)) {
232 for (bmask = 1; bmask < 0x08; bmask <<= 1) {
233 if (!(mbut & bmask))
234 continue;
235 fe2 = &ms->ms_bq[ms->ms_bq_idx++];
236 if (bmask == 1)
237 fe2->id = MS_RIGHT;
238 else if (bmask == 2)
239 fe2->id = MS_LEFT;
240 else fe2->id = MS_MIDDLE;
241 fe2->value = rel_ms->id & bmask ? VKEY_DOWN : VKEY_UP;
242 fe2->time = time;
243 }
244 }
245
246 /*
247 * Handle 3rd button emulation.
248 */
249 if (ms->ms_emul3b && ms->ms_bq_idx && (type != KBD_TIMEO_PKG)) {
250 /*
251 * If the middle button is pressed, any change to
252 * one of the other buttons releases all.
253 */
254 if ((ms->ms_buttons & 4) && (mbut & 3)) {
255 ms->ms_bq[0].id = MS_MIDDLE;
256 ms->ms_bq_idx = 1;
257 rel_ms->id = 0;
258 flush_buttons = 1;
259 goto out;
260 }
261 if (ms->ms_bq_idx == 2) {
262 if (ms->ms_bq[0].value == ms->ms_bq[1].value) {
263 /* Must be 2 button presses! */
264 ms->ms_bq[0].id = MS_MIDDLE;
265 ms->ms_bq_idx = 1;
266 rel_ms->id = 7;
267 }
268 }
269 else if (ms->ms_bq[0].value == VKEY_DOWN) {
270 timeout((FPV)ms_3b_delay, (void *)ms, 10);
271 goto out;
272 }
273 flush_buttons = 1;
274 }
275 out:
276 if (flush_buttons) {
277 int i;
278
279 for (i = 0; i < ms->ms_bq_idx; i++) {
280 if ((++put) % EV_QSIZE == get) {
281 ms->ms_bq_idx = 0;
282 put--;
283 goto out;
284 }
285 *fe = ms->ms_bq[i];
286 if (put >= EV_QSIZE) {
287 put = 0;
288 fe = &ms->ms_events.ev_q[0];
289 }
290 else fe++;
291 }
292 ms->ms_bq_idx = 0;
293 }
294 ms->ms_events.ev_put = put;
295 ms->ms_buttons = rel_ms->id;
296 splx(sps);
297 EV_WAKEUP(&ms->ms_events);
298 }
299
300 int
301 msopen(dev, flags, mode, p)
302 dev_t dev;
303 int flags, mode;
304 struct proc *p;
305 {
306 u_char report_ms_joy[] = { 0x14, 0x08 };
307 struct ms_softc *ms;
308 int unit;
309
310 unit = minor(dev);
311 ms = &ms_softc[unit];
312
313 if (unit >= NMOUSE)
314 return(EXDEV);
315
316 if (ms->ms_events.ev_io)
317 return(EBUSY);
318
319 ms->ms_events.ev_io = p;
320 ms->ms_dx = ms->ms_dy = 0;
321 ms->ms_buttons = 0;
322 ms->ms_bq[0].id = ms->ms_bq[1].id = 0;
323 ms->ms_bq_idx = 0;
324 ev_init(&ms->ms_events); /* may cause sleep */
325
326 /*
327 * Enable mouse reporting.
328 */
329 kbd_write(report_ms_joy, sizeof(report_ms_joy));
330 return(0);
331 }
332
333 int
334 msclose(dev, flags, mode, p)
335 dev_t dev;
336 int flags, mode;
337 struct proc *p;
338 {
339 u_char disable_ms_joy[] = { 0x12, 0x1a };
340 int unit;
341 struct ms_softc *ms;
342
343 unit = minor (dev);
344 ms = &ms_softc[unit];
345
346 /*
347 * Turn off mouse interrogation.
348 */
349 kbd_write(disable_ms_joy, sizeof(disable_ms_joy));
350 ev_fini(&ms->ms_events);
351 ms->ms_events.ev_io = NULL;
352 return(0);
353 }
354
355 int
356 msread(dev, uio, flags)
357 dev_t dev;
358 struct uio *uio;
359 int flags;
360 {
361 struct ms_softc *ms;
362
363 ms = &ms_softc[minor(dev)];
364 return(ev_read(&ms->ms_events, uio, flags));
365 }
366
367 int
368 msioctl(dev, cmd, data, flag, p)
369 dev_t dev;
370 u_long cmd;
371 register caddr_t data;
372 int flag;
373 struct proc *p;
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 TIOCSPGRP:
394 if (*(int *)data != ms->ms_events.ev_io->p_pgid)
395 return(EPERM);
396 return(0);
397 case VUIDGFORMAT: /* we only do firm_events */
398 *(int *)data = VUID_FIRM_EVENT;
399 return(0);
400 case VUIDSFORMAT:
401 if (*(int *)data != VUID_FIRM_EVENT)
402 return(EINVAL);
403 return(0);
404 }
405 return(ENOTTY);
406 }
407
408 int
409 msselect(dev, rw, p)
410 dev_t dev;
411 int rw;
412 struct proc *p;
413 {
414 struct ms_softc *ms;
415
416 ms = &ms_softc[minor(dev)];
417 return(ev_select(&ms->ms_events, rw, p));
418 }
419 #endif /* NMOUSE > 0 */
420