ms.c revision 1.1 1 /* $NetBSD: ms.c,v 1.1 1995/06/25 19:05:27 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 int
79 mouseattach(cnt)
80 int cnt;
81 {
82 printf("1 mouse configured\n");
83 return(NMOUSE);
84 }
85
86 /*
87 * Mouse specific packages produced by the keyboard. Currently, we only
88 * define the REL_MOUSE package, as this is the only one used.
89 */
90 typedef struct {
91 u_char id;
92 char dx;
93 char dy;
94 } REL_MOUSE;
95
96 #define IS_REL_MOUSE(id) (((u_int)(id) & 0xF8) == 0xF8)
97
98 static struct ms_softc {
99 u_char ms_buttons; /* button states */
100 struct evvar ms_events; /* event queue state */
101 int ms_dx; /* accumulated dx */
102 int ms_dy; /* accumulated dy */
103 } ms_softc[NMOUSE];
104
105 /*
106 * Note that we are called from the keyboard software interrupt!
107 */
108 void
109 mouse_soft(rel_ms, size)
110 REL_MOUSE *rel_ms;
111 int size;
112 {
113 struct ms_softc *ms = &ms_softc[0];
114 struct firm_event *fe;
115 int get, put;
116 int sps;
117 u_char mbut, bmask;
118
119 if (!IS_REL_MOUSE(rel_ms->id))
120 return; /* Probably some other message */
121 if (ms->ms_events.ev_io == NULL)
122 return;
123
124 sps = splev();
125 get = ms->ms_events.ev_get;
126 put = ms->ms_events.ev_put;
127 fe = &ms->ms_events.ev_q[put];
128
129 /*
130 * Button states are encoded in the lower 2 bits of 'id'
131 */
132 if (!(mbut = ((rel_ms->id & 3) ^ ms->ms_buttons)) && (put != get)) {
133 /*
134 * Compact dx/dy messages. Always generate an event when
135 * a button is pressed or the event queue is empty.
136 */
137 ms->ms_dx += rel_ms->dx;
138 ms->ms_dy += rel_ms->dy;
139 goto out;
140 }
141 rel_ms->dx += ms->ms_dx;
142 rel_ms->dy += ms->ms_dy;
143 ms->ms_dx = ms->ms_dy = 0;
144
145 /*
146 * Output location events _before_ button events ie. make sure
147 * the button is pressed at the correct location.
148 */
149 if (rel_ms->dx) {
150 if ((++put) % EV_QSIZE == get) {
151 put--;
152 goto out;
153 }
154 fe->id = LOC_X_DELTA;
155 fe->value = rel_ms->dx;
156 fe->time = time;
157 if (put >= EV_QSIZE) {
158 put = 0;
159 fe = &ms->ms_events.ev_q[0];
160 }
161 else fe++;
162 }
163 if (rel_ms->dy) {
164 if ((++put) % EV_QSIZE == get) {
165 put--;
166 goto out;
167 }
168 fe->id = LOC_Y_DELTA;
169 fe->value = rel_ms->dy;
170 fe->time = time;
171 if (put >= EV_QSIZE) {
172 put = 0;
173 fe = &ms->ms_events.ev_q[0];
174 }
175 else fe++;
176 }
177 if (mbut) {
178 for (bmask = 1; bmask < 0x04; bmask <<= 1) {
179 if (!(mbut & bmask))
180 continue;
181 if ((++put) % EV_QSIZE == get) {
182 put--;
183 goto out;
184 }
185 fe->id = bmask & 1 ? MS_RIGHT : MS_LEFT;
186 fe->value = rel_ms->id & bmask ? VKEY_DOWN : VKEY_UP;
187 fe->time = time;
188 if (put >= EV_QSIZE) {
189 put = 0;
190 fe = &ms->ms_events.ev_q[0];
191 }
192 else fe++;
193 }
194 }
195 out:
196 ms->ms_events.ev_put = put;
197 ms->ms_buttons = rel_ms->id & 3;
198 splx(sps);
199 EV_WAKEUP(&ms->ms_events);
200 }
201
202 int
203 msopen(dev, flags, mode, p)
204 dev_t dev;
205 int flags, mode;
206 struct proc *p;
207 {
208 u_char report_ms[] = { 0x08 };
209 struct ms_softc *ms;
210 int unit;
211
212 unit = minor(dev);
213 ms = &ms_softc[unit];
214
215 if (unit >= NMOUSE)
216 return(EXDEV);
217
218 if (ms->ms_events.ev_io)
219 return(EBUSY);
220
221 ms->ms_events.ev_io = p;
222 ms->ms_dx = ms->ms_dy = 0;
223 ev_init(&ms->ms_events); /* may cause sleep */
224
225 /*
226 * Enable mouse reporting.
227 */
228 kbd_write(report_ms, sizeof(report_ms));
229 return(0);
230 }
231
232 int
233 msclose(dev, flags, mode, p)
234 dev_t dev;
235 int flags, mode;
236 struct proc *p;
237 {
238 u_char disable_ms[] = { 0x12 };
239 int unit;
240 struct ms_softc *ms;
241
242 unit = minor (dev);
243 ms = &ms_softc[unit];
244
245 /*
246 * Turn off mouse interrogation.
247 */
248 kbd_write(disable_ms, sizeof(disable_ms));
249 ev_fini(&ms->ms_events);
250 ms->ms_events.ev_io = NULL;
251 return(0);
252 }
253
254 int
255 msread(dev, uio, flags)
256 dev_t dev;
257 struct uio *uio;
258 int flags;
259 {
260 struct ms_softc *ms;
261
262 ms = &ms_softc[minor(dev)];
263 return(ev_read(&ms->ms_events, uio, flags));
264 }
265
266 int
267 msioctl(dev, cmd, data, flag, p)
268 dev_t dev;
269 u_long cmd;
270 register caddr_t data;
271 int flag;
272 struct proc *p;
273 {
274 struct ms_softc *ms;
275 int unit;
276
277 unit = minor(dev);
278 ms = &ms_softc[unit];
279
280 switch (cmd) {
281 case FIONBIO: /* we will remove this someday (soon???) */
282 return(0);
283 case FIOASYNC:
284 ms->ms_events.ev_async = *(int *)data != 0;
285 return(0);
286 case TIOCSPGRP:
287 if (*(int *)data != ms->ms_events.ev_io->p_pgid)
288 return(EPERM);
289 return(0);
290 case VUIDGFORMAT: /* we only do firm_events */
291 *(int *)data = VUID_FIRM_EVENT;
292 return(0);
293 case VUIDSFORMAT:
294 if (*(int *)data != VUID_FIRM_EVENT)
295 return(EINVAL);
296 return(0);
297 }
298 return(ENOTTY);
299 }
300
301 int
302 msselect(dev, rw, p)
303 dev_t dev;
304 int rw;
305 struct proc *p;
306 {
307 struct ms_softc *ms;
308
309 ms = &ms_softc[minor(dev)];
310 return(ev_select(&ms->ms_events, rw, p));
311 }
312
313 #else /* NMOUSE > 0 */
314 /*
315 * Provide the hook to the keyboard driver.
316 */
317 void
318 mouse_soft(pkg, size)
319 void *pkg;
320 int size;
321 {
322 }
323 #endif /* NMOUSE > 0 */
324