ms.c revision 1.1 1 1.1 leo /* $NetBSD: ms.c,v 1.1 1995/06/25 19:05:27 leo Exp $
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Leo Weppelman.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * based on:
8 1.1 leo *
9 1.1 leo * Copyright (c) 1992, 1993
10 1.1 leo * The Regents of the University of California. All rights reserved.
11 1.1 leo *
12 1.1 leo * This software was developed by the Computer Systems Engineering group
13 1.1 leo * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
14 1.1 leo * contributed to Berkeley.
15 1.1 leo *
16 1.1 leo * All advertising materials mentioning features or use of this software
17 1.1 leo * must display the following acknowledgement:
18 1.1 leo * This product includes software developed by the University of
19 1.1 leo * California, Lawrence Berkeley Laboratory.
20 1.1 leo *
21 1.1 leo * Redistribution and use in source and binary forms, with or without
22 1.1 leo * modification, are permitted provided that the following conditions
23 1.1 leo * are met:
24 1.1 leo * 1. Redistributions of source code must retain the above copyright
25 1.1 leo * notice, this list of conditions and the following disclaimer.
26 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
27 1.1 leo * notice, this list of conditions and the following disclaimer in the
28 1.1 leo * documentation and/or other materials provided with the distribution.
29 1.1 leo * 3. All advertising materials mentioning features or use of this software
30 1.1 leo * must display the following acknowledgement:
31 1.1 leo * This product includes software developed by the University of
32 1.1 leo * California, Berkeley and its contributors.
33 1.1 leo * 4. Neither the name of the University nor the names of its contributors
34 1.1 leo * may be used to endorse or promote products derived from this software
35 1.1 leo * without specific prior written permission.
36 1.1 leo *
37 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 1.1 leo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 1.1 leo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 1.1 leo * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 1.1 leo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 1.1 leo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 1.1 leo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 1.1 leo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 1.1 leo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 1.1 leo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 1.1 leo * SUCH DAMAGE.
48 1.1 leo *
49 1.1 leo * @(#)ms.c 8.1 (Berkeley) 6/11/93
50 1.1 leo *
51 1.1 leo * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp (LBL)
52 1.1 leo */
53 1.1 leo
54 1.1 leo /*
55 1.1 leo * Mouse driver.
56 1.1 leo */
57 1.1 leo
58 1.1 leo #include <sys/param.h>
59 1.1 leo #include <sys/conf.h>
60 1.1 leo #include <sys/ioctl.h>
61 1.1 leo #include <sys/kernel.h>
62 1.1 leo #include <sys/proc.h>
63 1.1 leo #include <sys/systm.h>
64 1.1 leo #include <sys/tty.h>
65 1.1 leo
66 1.1 leo #include <atari/dev/event_var.h>
67 1.1 leo #include <atari/dev/vuid_event.h>
68 1.1 leo
69 1.1 leo #include "mouse.h"
70 1.1 leo #if NMOUSE > 0
71 1.1 leo
72 1.1 leo /* there's really no more physical ports on an atari. */
73 1.1 leo #if NMOUSE > 1
74 1.1 leo #undef NMOUSE
75 1.1 leo #define NMOUSE 1
76 1.1 leo #endif
77 1.1 leo
78 1.1 leo int
79 1.1 leo mouseattach(cnt)
80 1.1 leo int cnt;
81 1.1 leo {
82 1.1 leo printf("1 mouse configured\n");
83 1.1 leo return(NMOUSE);
84 1.1 leo }
85 1.1 leo
86 1.1 leo /*
87 1.1 leo * Mouse specific packages produced by the keyboard. Currently, we only
88 1.1 leo * define the REL_MOUSE package, as this is the only one used.
89 1.1 leo */
90 1.1 leo typedef struct {
91 1.1 leo u_char id;
92 1.1 leo char dx;
93 1.1 leo char dy;
94 1.1 leo } REL_MOUSE;
95 1.1 leo
96 1.1 leo #define IS_REL_MOUSE(id) (((u_int)(id) & 0xF8) == 0xF8)
97 1.1 leo
98 1.1 leo static struct ms_softc {
99 1.1 leo u_char ms_buttons; /* button states */
100 1.1 leo struct evvar ms_events; /* event queue state */
101 1.1 leo int ms_dx; /* accumulated dx */
102 1.1 leo int ms_dy; /* accumulated dy */
103 1.1 leo } ms_softc[NMOUSE];
104 1.1 leo
105 1.1 leo /*
106 1.1 leo * Note that we are called from the keyboard software interrupt!
107 1.1 leo */
108 1.1 leo void
109 1.1 leo mouse_soft(rel_ms, size)
110 1.1 leo REL_MOUSE *rel_ms;
111 1.1 leo int size;
112 1.1 leo {
113 1.1 leo struct ms_softc *ms = &ms_softc[0];
114 1.1 leo struct firm_event *fe;
115 1.1 leo int get, put;
116 1.1 leo int sps;
117 1.1 leo u_char mbut, bmask;
118 1.1 leo
119 1.1 leo if (!IS_REL_MOUSE(rel_ms->id))
120 1.1 leo return; /* Probably some other message */
121 1.1 leo if (ms->ms_events.ev_io == NULL)
122 1.1 leo return;
123 1.1 leo
124 1.1 leo sps = splev();
125 1.1 leo get = ms->ms_events.ev_get;
126 1.1 leo put = ms->ms_events.ev_put;
127 1.1 leo fe = &ms->ms_events.ev_q[put];
128 1.1 leo
129 1.1 leo /*
130 1.1 leo * Button states are encoded in the lower 2 bits of 'id'
131 1.1 leo */
132 1.1 leo if (!(mbut = ((rel_ms->id & 3) ^ ms->ms_buttons)) && (put != get)) {
133 1.1 leo /*
134 1.1 leo * Compact dx/dy messages. Always generate an event when
135 1.1 leo * a button is pressed or the event queue is empty.
136 1.1 leo */
137 1.1 leo ms->ms_dx += rel_ms->dx;
138 1.1 leo ms->ms_dy += rel_ms->dy;
139 1.1 leo goto out;
140 1.1 leo }
141 1.1 leo rel_ms->dx += ms->ms_dx;
142 1.1 leo rel_ms->dy += ms->ms_dy;
143 1.1 leo ms->ms_dx = ms->ms_dy = 0;
144 1.1 leo
145 1.1 leo /*
146 1.1 leo * Output location events _before_ button events ie. make sure
147 1.1 leo * the button is pressed at the correct location.
148 1.1 leo */
149 1.1 leo if (rel_ms->dx) {
150 1.1 leo if ((++put) % EV_QSIZE == get) {
151 1.1 leo put--;
152 1.1 leo goto out;
153 1.1 leo }
154 1.1 leo fe->id = LOC_X_DELTA;
155 1.1 leo fe->value = rel_ms->dx;
156 1.1 leo fe->time = time;
157 1.1 leo if (put >= EV_QSIZE) {
158 1.1 leo put = 0;
159 1.1 leo fe = &ms->ms_events.ev_q[0];
160 1.1 leo }
161 1.1 leo else fe++;
162 1.1 leo }
163 1.1 leo if (rel_ms->dy) {
164 1.1 leo if ((++put) % EV_QSIZE == get) {
165 1.1 leo put--;
166 1.1 leo goto out;
167 1.1 leo }
168 1.1 leo fe->id = LOC_Y_DELTA;
169 1.1 leo fe->value = rel_ms->dy;
170 1.1 leo fe->time = time;
171 1.1 leo if (put >= EV_QSIZE) {
172 1.1 leo put = 0;
173 1.1 leo fe = &ms->ms_events.ev_q[0];
174 1.1 leo }
175 1.1 leo else fe++;
176 1.1 leo }
177 1.1 leo if (mbut) {
178 1.1 leo for (bmask = 1; bmask < 0x04; bmask <<= 1) {
179 1.1 leo if (!(mbut & bmask))
180 1.1 leo continue;
181 1.1 leo if ((++put) % EV_QSIZE == get) {
182 1.1 leo put--;
183 1.1 leo goto out;
184 1.1 leo }
185 1.1 leo fe->id = bmask & 1 ? MS_RIGHT : MS_LEFT;
186 1.1 leo fe->value = rel_ms->id & bmask ? VKEY_DOWN : VKEY_UP;
187 1.1 leo fe->time = time;
188 1.1 leo if (put >= EV_QSIZE) {
189 1.1 leo put = 0;
190 1.1 leo fe = &ms->ms_events.ev_q[0];
191 1.1 leo }
192 1.1 leo else fe++;
193 1.1 leo }
194 1.1 leo }
195 1.1 leo out:
196 1.1 leo ms->ms_events.ev_put = put;
197 1.1 leo ms->ms_buttons = rel_ms->id & 3;
198 1.1 leo splx(sps);
199 1.1 leo EV_WAKEUP(&ms->ms_events);
200 1.1 leo }
201 1.1 leo
202 1.1 leo int
203 1.1 leo msopen(dev, flags, mode, p)
204 1.1 leo dev_t dev;
205 1.1 leo int flags, mode;
206 1.1 leo struct proc *p;
207 1.1 leo {
208 1.1 leo u_char report_ms[] = { 0x08 };
209 1.1 leo struct ms_softc *ms;
210 1.1 leo int unit;
211 1.1 leo
212 1.1 leo unit = minor(dev);
213 1.1 leo ms = &ms_softc[unit];
214 1.1 leo
215 1.1 leo if (unit >= NMOUSE)
216 1.1 leo return(EXDEV);
217 1.1 leo
218 1.1 leo if (ms->ms_events.ev_io)
219 1.1 leo return(EBUSY);
220 1.1 leo
221 1.1 leo ms->ms_events.ev_io = p;
222 1.1 leo ms->ms_dx = ms->ms_dy = 0;
223 1.1 leo ev_init(&ms->ms_events); /* may cause sleep */
224 1.1 leo
225 1.1 leo /*
226 1.1 leo * Enable mouse reporting.
227 1.1 leo */
228 1.1 leo kbd_write(report_ms, sizeof(report_ms));
229 1.1 leo return(0);
230 1.1 leo }
231 1.1 leo
232 1.1 leo int
233 1.1 leo msclose(dev, flags, mode, p)
234 1.1 leo dev_t dev;
235 1.1 leo int flags, mode;
236 1.1 leo struct proc *p;
237 1.1 leo {
238 1.1 leo u_char disable_ms[] = { 0x12 };
239 1.1 leo int unit;
240 1.1 leo struct ms_softc *ms;
241 1.1 leo
242 1.1 leo unit = minor (dev);
243 1.1 leo ms = &ms_softc[unit];
244 1.1 leo
245 1.1 leo /*
246 1.1 leo * Turn off mouse interrogation.
247 1.1 leo */
248 1.1 leo kbd_write(disable_ms, sizeof(disable_ms));
249 1.1 leo ev_fini(&ms->ms_events);
250 1.1 leo ms->ms_events.ev_io = NULL;
251 1.1 leo return(0);
252 1.1 leo }
253 1.1 leo
254 1.1 leo int
255 1.1 leo msread(dev, uio, flags)
256 1.1 leo dev_t dev;
257 1.1 leo struct uio *uio;
258 1.1 leo int flags;
259 1.1 leo {
260 1.1 leo struct ms_softc *ms;
261 1.1 leo
262 1.1 leo ms = &ms_softc[minor(dev)];
263 1.1 leo return(ev_read(&ms->ms_events, uio, flags));
264 1.1 leo }
265 1.1 leo
266 1.1 leo int
267 1.1 leo msioctl(dev, cmd, data, flag, p)
268 1.1 leo dev_t dev;
269 1.1 leo u_long cmd;
270 1.1 leo register caddr_t data;
271 1.1 leo int flag;
272 1.1 leo struct proc *p;
273 1.1 leo {
274 1.1 leo struct ms_softc *ms;
275 1.1 leo int unit;
276 1.1 leo
277 1.1 leo unit = minor(dev);
278 1.1 leo ms = &ms_softc[unit];
279 1.1 leo
280 1.1 leo switch (cmd) {
281 1.1 leo case FIONBIO: /* we will remove this someday (soon???) */
282 1.1 leo return(0);
283 1.1 leo case FIOASYNC:
284 1.1 leo ms->ms_events.ev_async = *(int *)data != 0;
285 1.1 leo return(0);
286 1.1 leo case TIOCSPGRP:
287 1.1 leo if (*(int *)data != ms->ms_events.ev_io->p_pgid)
288 1.1 leo return(EPERM);
289 1.1 leo return(0);
290 1.1 leo case VUIDGFORMAT: /* we only do firm_events */
291 1.1 leo *(int *)data = VUID_FIRM_EVENT;
292 1.1 leo return(0);
293 1.1 leo case VUIDSFORMAT:
294 1.1 leo if (*(int *)data != VUID_FIRM_EVENT)
295 1.1 leo return(EINVAL);
296 1.1 leo return(0);
297 1.1 leo }
298 1.1 leo return(ENOTTY);
299 1.1 leo }
300 1.1 leo
301 1.1 leo int
302 1.1 leo msselect(dev, rw, p)
303 1.1 leo dev_t dev;
304 1.1 leo int rw;
305 1.1 leo struct proc *p;
306 1.1 leo {
307 1.1 leo struct ms_softc *ms;
308 1.1 leo
309 1.1 leo ms = &ms_softc[minor(dev)];
310 1.1 leo return(ev_select(&ms->ms_events, rw, p));
311 1.1 leo }
312 1.1 leo
313 1.1 leo #else /* NMOUSE > 0 */
314 1.1 leo /*
315 1.1 leo * Provide the hook to the keyboard driver.
316 1.1 leo */
317 1.1 leo void
318 1.1 leo mouse_soft(pkg, size)
319 1.1 leo void *pkg;
320 1.1 leo int size;
321 1.1 leo {
322 1.1 leo }
323 1.1 leo #endif /* NMOUSE > 0 */
324