ms.c revision 1.1 1 1.1 oki /* $NetBSD: ms.c,v 1.1 1996/05/05 12:17:06 oki Exp $ */
2 1.1 oki
3 1.1 oki /*
4 1.1 oki * Copyright (c) 1992, 1993
5 1.1 oki * The Regents of the University of California. All rights reserved.
6 1.1 oki *
7 1.1 oki * This software was developed by the Computer Systems Engineering group
8 1.1 oki * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 oki * contributed to Berkeley.
10 1.1 oki *
11 1.1 oki * All advertising materials mentioning features or use of this software
12 1.1 oki * must display the following acknowledgement:
13 1.1 oki * This product includes software developed by the University of
14 1.1 oki * California, Lawrence Berkeley Laboratory.
15 1.1 oki *
16 1.1 oki * Redistribution and use in source and binary forms, with or without
17 1.1 oki * modification, are permitted provided that the following conditions
18 1.1 oki * are met:
19 1.1 oki * 1. Redistributions of source code must retain the above copyright
20 1.1 oki * notice, this list of conditions and the following disclaimer.
21 1.1 oki * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 oki * notice, this list of conditions and the following disclaimer in the
23 1.1 oki * documentation and/or other materials provided with the distribution.
24 1.1 oki * 3. All advertising materials mentioning features or use of this software
25 1.1 oki * must display the following acknowledgement:
26 1.1 oki * This product includes software developed by the University of
27 1.1 oki * California, Berkeley and its contributors.
28 1.1 oki * 4. Neither the name of the University nor the names of its contributors
29 1.1 oki * may be used to endorse or promote products derived from this software
30 1.1 oki * without specific prior written permission.
31 1.1 oki *
32 1.1 oki * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.1 oki * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.1 oki * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.1 oki * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.1 oki * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 oki * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 oki * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 oki * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.1 oki * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.1 oki * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.1 oki * SUCH DAMAGE.
43 1.1 oki *
44 1.1 oki * @(#)ms.c 8.1 (Berkeley) 6/11/93
45 1.1 oki */
46 1.1 oki
47 1.1 oki /*
48 1.1 oki * Mouse driver.
49 1.1 oki */
50 1.1 oki
51 1.1 oki #include <sys/param.h>
52 1.1 oki #include <sys/conf.h>
53 1.1 oki #include <sys/ioctl.h>
54 1.1 oki #include <sys/kernel.h>
55 1.1 oki #include <sys/proc.h>
56 1.1 oki #include <sys/syslog.h>
57 1.1 oki #include <sys/systm.h>
58 1.1 oki #include <sys/tty.h>
59 1.1 oki
60 1.1 oki #include <x68k/dev/event_var.h>
61 1.1 oki #include <machine/vuid_event.h>
62 1.1 oki
63 1.1 oki #include <x68k/x68k/iodevice.h>
64 1.1 oki
65 1.1 oki /*
66 1.1 oki * Mouse state. A SHARP X1/X680x0 mouse is a fairly simple device,
67 1.1 oki * producing three-byte blobs of the form:
68 1.1 oki *
69 1.1 oki * b dx dy
70 1.1 oki *
71 1.1 oki * where b is the button state, encoded as 0x80|(buttons)---there are
72 1.1 oki * two buttons (2=left, 1=right)---and dx,dy are X and Y delta values.
73 1.1 oki */
74 1.1 oki struct ms_softc {
75 1.1 oki short ms_byteno; /* input byte number, for decode */
76 1.1 oki char ms_mb; /* mouse button state */
77 1.1 oki char ms_ub; /* user button state */
78 1.1 oki int ms_dx; /* delta-x */
79 1.1 oki int ms_dy; /* delta-y */
80 1.1 oki struct tty *ms_mouse; /* downlink for output to mouse */
81 1.1 oki void (*ms_open) __P((struct tty *)); /* enable dataflow */
82 1.1 oki void (*ms_close) __P((struct tty *));/* disable dataflow */
83 1.1 oki volatile int ms_ready; /* event queue is ready */
84 1.1 oki struct evvar ms_events; /* event queue state */
85 1.1 oki } ms_softc;
86 1.1 oki
87 1.1 oki /*
88 1.1 oki * Attach the mouse serial (down-link) interface.
89 1.1 oki * Do we need to set it to 4800 baud, 8 bits?
90 1.1 oki * Test by power cycling and not booting Human68k before BSD?
91 1.1 oki */
92 1.1 oki void
93 1.1 oki ms_serial(tp, iopen, iclose)
94 1.1 oki struct tty *tp;
95 1.1 oki void (*iopen)(), (*iclose)();
96 1.1 oki {
97 1.1 oki
98 1.1 oki ms_softc.ms_mouse = tp;
99 1.1 oki ms_softc.ms_open = iopen;
100 1.1 oki ms_softc.ms_close = iclose;
101 1.1 oki }
102 1.1 oki
103 1.1 oki void
104 1.1 oki ms_modem(onoff)
105 1.1 oki register int onoff;
106 1.1 oki {
107 1.1 oki static int oonoff;
108 1.1 oki
109 1.1 oki if (ms_softc.ms_ready == 1) {
110 1.1 oki if (ms_softc.ms_byteno == -1)
111 1.1 oki ms_softc.ms_byteno = onoff = 0;
112 1.1 oki if (oonoff != onoff) {
113 1.1 oki zs_msmodem(onoff);
114 1.1 oki oonoff = onoff;
115 1.1 oki }
116 1.1 oki }
117 1.1 oki }
118 1.1 oki
119 1.1 oki void
120 1.1 oki ms_rint(c)
121 1.1 oki register int c;
122 1.1 oki {
123 1.1 oki register struct firm_event *fe;
124 1.1 oki register struct ms_softc *ms = &ms_softc;
125 1.1 oki register int mb, ub, d, get, put, any;
126 1.1 oki static const char to_one[] = { 1, 2, 3 };
127 1.1 oki static const int to_id[] = { MS_LEFT, MS_RIGHT, MS_MIDDLE };
128 1.1 oki
129 1.1 oki /*
130 1.1 oki * Discard input if not ready. Drop sync on parity or framing
131 1.1 oki * error; gain sync on button byte.
132 1.1 oki */
133 1.1 oki if (ms->ms_ready == 0)
134 1.1 oki return;
135 1.1 oki if (c & (TTY_FE|TTY_PE)) {
136 1.1 oki log(LOG_WARNING,
137 1.1 oki "mouse input parity or framing error (0x%x)\n", c);
138 1.1 oki ms->ms_byteno = -1;
139 1.1 oki return;
140 1.1 oki }
141 1.1 oki
142 1.1 oki /*
143 1.1 oki * Run the decode loop, adding to the current information.
144 1.1 oki * We add, rather than replace, deltas, so that if the event queue
145 1.1 oki * fills, we accumulate data for when it opens up again.
146 1.1 oki */
147 1.1 oki switch (ms->ms_byteno) {
148 1.1 oki
149 1.1 oki case -1:
150 1.1 oki return;
151 1.1 oki
152 1.1 oki case 0:
153 1.1 oki /* buttons */
154 1.1 oki ms->ms_byteno = 1;
155 1.1 oki ms->ms_mb = c & 0x7;
156 1.1 oki return;
157 1.1 oki
158 1.1 oki case 1:
159 1.1 oki /* delta-x */
160 1.1 oki ms->ms_byteno = 2;
161 1.1 oki ms->ms_dx += (char)c;
162 1.1 oki return;
163 1.1 oki
164 1.1 oki case 2:
165 1.1 oki /* delta-y */
166 1.1 oki ms->ms_byteno = -1 /* wait for button-byte again */;
167 1.1 oki ms->ms_dy += (char)c;
168 1.1 oki break;
169 1.1 oki
170 1.1 oki default:
171 1.1 oki panic("ms_rint");
172 1.1 oki /* NOTREACHED */
173 1.1 oki }
174 1.1 oki
175 1.1 oki /*
176 1.1 oki * We have at least one event (mouse button, delta-X, or
177 1.1 oki * delta-Y; possibly all three, and possibly three separate
178 1.1 oki * button events). Deliver these events until we are out
179 1.1 oki * of changes or out of room. As events get delivered,
180 1.1 oki * mark them `unchanged'.
181 1.1 oki */
182 1.1 oki any = 0;
183 1.1 oki get = ms->ms_events.ev_get;
184 1.1 oki put = ms->ms_events.ev_put;
185 1.1 oki fe = &ms->ms_events.ev_q[put];
186 1.1 oki
187 1.1 oki /* NEXT prepares to put the next event, backing off if necessary */
188 1.1 oki #define NEXT \
189 1.1 oki if ((++put) % EV_QSIZE == get) { \
190 1.1 oki put--; \
191 1.1 oki goto out; \
192 1.1 oki }
193 1.1 oki /* ADVANCE completes the `put' of the event */
194 1.1 oki #define ADVANCE \
195 1.1 oki fe++; \
196 1.1 oki if (put >= EV_QSIZE) { \
197 1.1 oki put = 0; \
198 1.1 oki fe = &ms->ms_events.ev_q[0]; \
199 1.1 oki } \
200 1.1 oki any = 1
201 1.1 oki
202 1.1 oki mb = ms->ms_mb;
203 1.1 oki ub = ms->ms_ub;
204 1.1 oki while ((d = mb ^ ub) != 0) {
205 1.1 oki /*
206 1.1 oki * Mouse button change. Convert up to three changes
207 1.1 oki * to the `first' change, and drop it into the event queue.
208 1.1 oki */
209 1.1 oki NEXT;
210 1.1 oki d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
211 1.1 oki fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
212 1.1 oki fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
213 1.1 oki fe->time = time;
214 1.1 oki ADVANCE;
215 1.1 oki ub ^= d;
216 1.1 oki }
217 1.1 oki if (ms->ms_dx) {
218 1.1 oki NEXT;
219 1.1 oki fe->id = LOC_X_DELTA;
220 1.1 oki fe->value = ms->ms_dx;
221 1.1 oki fe->time = time;
222 1.1 oki ADVANCE;
223 1.1 oki ms->ms_dx = 0;
224 1.1 oki }
225 1.1 oki if (ms->ms_dy) {
226 1.1 oki NEXT;
227 1.1 oki fe->id = LOC_Y_DELTA;
228 1.1 oki fe->value = -ms->ms_dy; /* XXX? */
229 1.1 oki fe->time = time;
230 1.1 oki ADVANCE;
231 1.1 oki ms->ms_dy = 0;
232 1.1 oki }
233 1.1 oki out:
234 1.1 oki if (any) {
235 1.1 oki ms->ms_ub = ub;
236 1.1 oki ms->ms_events.ev_put = put;
237 1.1 oki EV_WAKEUP(&ms->ms_events);
238 1.1 oki }
239 1.1 oki }
240 1.1 oki
241 1.1 oki int
242 1.1 oki msopen(dev, flags, mode, p)
243 1.1 oki dev_t dev;
244 1.1 oki int flags, mode;
245 1.1 oki struct proc *p;
246 1.1 oki {
247 1.1 oki int s, error;
248 1.1 oki
249 1.1 oki if (ms_softc.ms_events.ev_io)
250 1.1 oki return (EBUSY);
251 1.1 oki ms_softc.ms_events.ev_io = p;
252 1.1 oki ev_init(&ms_softc.ms_events); /* may cause sleep */
253 1.1 oki ms_softc.ms_ready = 1; /* start accepting events */
254 1.1 oki (*ms_softc.ms_open)(ms_softc.ms_mouse);
255 1.1 oki return (0);
256 1.1 oki }
257 1.1 oki int
258 1.1 oki msclose(dev, flags, mode, p)
259 1.1 oki dev_t dev;
260 1.1 oki int flags, mode;
261 1.1 oki struct proc *p;
262 1.1 oki {
263 1.1 oki
264 1.1 oki ms_modem(0);
265 1.1 oki ms_softc.ms_ready = 0; /* stop accepting events */
266 1.1 oki ev_fini(&ms_softc.ms_events);
267 1.1 oki (*ms_softc.ms_close)(ms_softc.ms_mouse);
268 1.1 oki ms_softc.ms_events.ev_io = NULL;
269 1.1 oki return (0);
270 1.1 oki }
271 1.1 oki
272 1.1 oki int
273 1.1 oki msread(dev, uio, flags)
274 1.1 oki dev_t dev;
275 1.1 oki struct uio *uio;
276 1.1 oki int flags;
277 1.1 oki {
278 1.1 oki
279 1.1 oki return (ev_read(&ms_softc.ms_events, uio, flags));
280 1.1 oki }
281 1.1 oki
282 1.1 oki /* this routine should not exist, but is convenient to write here for now */
283 1.1 oki int
284 1.1 oki mswrite(dev, uio, flags)
285 1.1 oki dev_t dev;
286 1.1 oki struct uio *uio;
287 1.1 oki int flags;
288 1.1 oki {
289 1.1 oki
290 1.1 oki return (EOPNOTSUPP);
291 1.1 oki }
292 1.1 oki
293 1.1 oki int
294 1.1 oki msioctl(dev, cmd, data, flag, p)
295 1.1 oki dev_t dev;
296 1.1 oki u_long cmd;
297 1.1 oki register caddr_t data;
298 1.1 oki int flag;
299 1.1 oki struct proc *p;
300 1.1 oki {
301 1.1 oki int s;
302 1.1 oki
303 1.1 oki switch (cmd) {
304 1.1 oki
305 1.1 oki case FIONBIO: /* we will remove this someday (soon???) */
306 1.1 oki return (0);
307 1.1 oki
308 1.1 oki case FIOASYNC:
309 1.1 oki ms_softc.ms_events.ev_async = *(int *)data != 0;
310 1.1 oki return (0);
311 1.1 oki
312 1.1 oki case TIOCSPGRP:
313 1.1 oki if (*(int *)data != ms_softc.ms_events.ev_io->p_pgid)
314 1.1 oki return (EPERM);
315 1.1 oki return (0);
316 1.1 oki
317 1.1 oki case VUIDGFORMAT:
318 1.1 oki /* we only do firm_events */
319 1.1 oki *(int *)data = VUID_FIRM_EVENT;
320 1.1 oki return (0);
321 1.1 oki
322 1.1 oki case VUIDSFORMAT:
323 1.1 oki if (*(int *)data != VUID_FIRM_EVENT)
324 1.1 oki return (EINVAL);
325 1.1 oki return (0);
326 1.1 oki }
327 1.1 oki return (ENOTTY);
328 1.1 oki }
329 1.1 oki
330 1.1 oki int
331 1.1 oki msselect(dev, rw, p)
332 1.1 oki dev_t dev;
333 1.1 oki int rw;
334 1.1 oki struct proc *p;
335 1.1 oki {
336 1.1 oki
337 1.1 oki return (ev_select(&ms_softc.ms_events, rw, p));
338 1.1 oki }
339 1.1 oki
340 1.1 oki mouseattach(){} /* XXX pseudo-device */
341