ms.c revision 1.18.12.4 1 1.18.12.1 nathanw /* $NetBSD: ms.c,v 1.18.12.4 2002/11/11 21:56:19 nathanw Exp $ */
2 1.5 cgd
3 1.1 mw /*
4 1.1 mw * based on:
5 1.1 mw *
6 1.1 mw * Copyright (c) 1992, 1993
7 1.1 mw * The Regents of the University of California. All rights reserved.
8 1.1 mw *
9 1.1 mw * This software was developed by the Computer Systems Engineering group
10 1.1 mw * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11 1.1 mw * contributed to Berkeley.
12 1.1 mw *
13 1.1 mw * All advertising materials mentioning features or use of this software
14 1.1 mw * must display the following acknowledgement:
15 1.1 mw * This product includes software developed by the University of
16 1.1 mw * California, Lawrence Berkeley Laboratory.
17 1.1 mw *
18 1.1 mw * Redistribution and use in source and binary forms, with or without
19 1.1 mw * modification, are permitted provided that the following conditions
20 1.1 mw * are met:
21 1.1 mw * 1. Redistributions of source code must retain the above copyright
22 1.1 mw * notice, this list of conditions and the following disclaimer.
23 1.1 mw * 2. Redistributions in binary form must reproduce the above copyright
24 1.1 mw * notice, this list of conditions and the following disclaimer in the
25 1.1 mw * documentation and/or other materials provided with the distribution.
26 1.1 mw * 3. All advertising materials mentioning features or use of this software
27 1.1 mw * must display the following acknowledgement:
28 1.1 mw * This product includes software developed by the University of
29 1.1 mw * California, Berkeley and its contributors.
30 1.1 mw * 4. Neither the name of the University nor the names of its contributors
31 1.1 mw * may be used to endorse or promote products derived from this software
32 1.1 mw * without specific prior written permission.
33 1.1 mw *
34 1.1 mw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 1.1 mw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 1.1 mw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 1.1 mw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 1.1 mw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 1.1 mw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 1.1 mw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 1.1 mw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 1.1 mw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 1.1 mw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 1.1 mw * SUCH DAMAGE.
45 1.1 mw *
46 1.1 mw * @(#)ms.c 8.1 (Berkeley) 6/11/93
47 1.1 mw *
48 1.5 cgd * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp (LBL)
49 1.1 mw */
50 1.1 mw
51 1.18.12.1 nathanw #include <sys/cdefs.h>
52 1.18.12.1 nathanw __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.18.12.4 2002/11/11 21:56:19 nathanw Exp $");
53 1.18.12.1 nathanw
54 1.1 mw /*
55 1.1 mw * Mouse driver.
56 1.1 mw */
57 1.1 mw
58 1.2 chopps #include <sys/param.h>
59 1.9 is #include <sys/device.h>
60 1.2 chopps #include <sys/ioctl.h>
61 1.2 chopps #include <sys/kernel.h>
62 1.2 chopps #include <sys/proc.h>
63 1.2 chopps #include <sys/syslog.h>
64 1.2 chopps #include <sys/systm.h>
65 1.16 thorpej #include <sys/callout.h>
66 1.2 chopps #include <sys/tty.h>
67 1.8 veego #include <sys/signalvar.h>
68 1.18.12.2 nathanw #include <sys/conf.h>
69 1.1 mw
70 1.2 chopps #include <amiga/dev/event_var.h>
71 1.2 chopps #include <amiga/dev/vuid_event.h>
72 1.1 mw
73 1.2 chopps #include <amiga/amiga/custom.h>
74 1.2 chopps #include <amiga/amiga/cia.h>
75 1.9 is #include <amiga/amiga/device.h>
76 1.1 mw
77 1.18.12.1 nathanw void msattach(struct device *, struct device *, void *);
78 1.18.12.1 nathanw int msmatch(struct device *, struct cfdata *, void *);
79 1.1 mw
80 1.17 kleink /* per-port state */
81 1.17 kleink struct ms_port {
82 1.17 kleink int ms_portno; /* which hardware port, for msintr() */
83 1.9 is
84 1.17 kleink struct callout ms_intr_ch;
85 1.16 thorpej
86 1.9 is u_char ms_horc; /* horizontal counter on last scan */
87 1.9 is u_char ms_verc; /* vertical counter on last scan */
88 1.9 is char ms_mb; /* mouse button state */
89 1.9 is char ms_ub; /* user button state */
90 1.9 is int ms_dx; /* delta-x */
91 1.9 is int ms_dy; /* delta-y */
92 1.9 is volatile int ms_ready; /* event queue is ready */
93 1.9 is struct evvar ms_events; /* event queue state */
94 1.9 is };
95 1.9 is
96 1.17 kleink #define MS_NPORTS 2
97 1.17 kleink
98 1.17 kleink struct ms_softc {
99 1.17 kleink struct device sc_dev; /* base device */
100 1.17 kleink struct ms_port sc_ports[MS_NPORTS];
101 1.17 kleink };
102 1.17 kleink
103 1.18.12.3 nathanw CFATTACH_DECL(ms, sizeof(struct ms_softc),
104 1.18.12.3 nathanw msmatch, msattach, NULL, NULL);
105 1.9 is
106 1.18.12.1 nathanw void msintr(void *);
107 1.18.12.1 nathanw void ms_enable(struct ms_port *);
108 1.18.12.1 nathanw void ms_disable(struct ms_port *);
109 1.17 kleink
110 1.15 thorpej extern struct cfdriver ms_cd;
111 1.18.12.2 nathanw
112 1.18.12.2 nathanw dev_type_open(msopen);
113 1.18.12.2 nathanw dev_type_close(msclose);
114 1.18.12.2 nathanw dev_type_read(msread);
115 1.18.12.2 nathanw dev_type_ioctl(msioctl);
116 1.18.12.2 nathanw dev_type_poll(mspoll);
117 1.18.12.4 nathanw dev_type_kqfilter(mskqfilter);
118 1.18.12.2 nathanw
119 1.18.12.2 nathanw const struct cdevsw ms_cdevsw = {
120 1.18.12.2 nathanw msopen, msclose, msread, nowrite, msioctl,
121 1.18.12.4 nathanw nostop, notty, mspoll, nommap, mskqfilter,
122 1.18.12.2 nathanw };
123 1.1 mw
124 1.17 kleink #define MS_UNIT(d) ((minor(d) & ~0x1) >> 1)
125 1.17 kleink #define MS_PORT(d) (minor(d) & 0x1)
126 1.17 kleink
127 1.17 kleink /*
128 1.17 kleink * Given a dev_t, return a pointer to the port's hardware state.
129 1.17 kleink * Assumes the unit to be valid, so do *not* utilize this in msopen().
130 1.17 kleink */
131 1.17 kleink #define MS_DEV2MSPORT(d) \
132 1.17 kleink (&(((struct ms_softc *)getsoftc(ms_cd, MS_UNIT(d)))->sc_ports[MS_PORT(d)]))
133 1.17 kleink
134 1.3 chopps int
135 1.18.12.1 nathanw msmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
136 1.3 chopps {
137 1.17 kleink static int ms_matched = 0;
138 1.9 is
139 1.17 kleink /* Allow only one instance. */
140 1.17 kleink if (!matchname((char *)auxp, "ms") || ms_matched)
141 1.17 kleink return 0;
142 1.9 is
143 1.17 kleink ms_matched = 1;
144 1.17 kleink return 1;
145 1.3 chopps }
146 1.1 mw
147 1.9 is void
148 1.18.12.1 nathanw msattach(struct device *pdp, struct device *dp, void *auxp)
149 1.9 is {
150 1.16 thorpej struct ms_softc *sc = (void *) dp;
151 1.17 kleink int i;
152 1.16 thorpej
153 1.13 christos printf("\n");
154 1.17 kleink for (i = 0; i < MS_NPORTS; i++) {
155 1.17 kleink sc->sc_ports[i].ms_portno = i;
156 1.17 kleink callout_init(&sc->sc_ports[i].ms_intr_ch);
157 1.17 kleink }
158 1.9 is }
159 1.9 is
160 1.3 chopps /*
161 1.3 chopps * Amiga mice are hooked up to one of the two "game" ports, where
162 1.3 chopps * the main mouse is usually on the first port, and port 2 can
163 1.3 chopps * be used by a joystick. Nevertheless, we support two mouse
164 1.3 chopps * devices, /dev/mouse0 and /dev/mouse1 (with a link of /dev/mouse to
165 1.3 chopps * the device that represents the port of the mouse in use).
166 1.3 chopps */
167 1.1 mw
168 1.3 chopps /*
169 1.17 kleink * enable scanner, called when someone opens the port.
170 1.3 chopps */
171 1.1 mw void
172 1.18.12.1 nathanw ms_enable(struct ms_port *ms)
173 1.1 mw {
174 1.10 is
175 1.18.12.1 nathanw /*
176 1.3 chopps * use this as flag to the "interrupt" to tell it when to
177 1.3 chopps * shut off (when it's reset to 0).
178 1.3 chopps */
179 1.3 chopps ms->ms_ready = 1;
180 1.1 mw
181 1.17 kleink callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
182 1.1 mw }
183 1.1 mw
184 1.3 chopps /*
185 1.3 chopps * disable scanner. Just set ms_ready to 0, and after the next
186 1.3 chopps * timeout taken, no further timeouts will be initiated.
187 1.3 chopps */
188 1.1 mw void
189 1.18.12.1 nathanw ms_disable(struct ms_port *ms)
190 1.1 mw {
191 1.3 chopps int s;
192 1.1 mw
193 1.3 chopps s = splhigh ();
194 1.3 chopps ms->ms_ready = 0;
195 1.3 chopps /*
196 1.3 chopps * sync with the interrupt
197 1.3 chopps */
198 1.3 chopps tsleep(ms, PZERO - 1, "mouse-disable", 0);
199 1.3 chopps splx(s);
200 1.1 mw }
201 1.1 mw
202 1.1 mw
203 1.18.12.1 nathanw /*
204 1.3 chopps * we're emulating a mousesystems serial mouse here..
205 1.3 chopps */
206 1.1 mw void
207 1.18.12.1 nathanw msintr(void *arg)
208 1.1 mw {
209 1.3 chopps static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
210 1.3 chopps static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
211 1.17 kleink struct ms_port *ms = arg;
212 1.3 chopps struct firm_event *fe;
213 1.17 kleink int mb, ub, d, get, put, any, port;
214 1.3 chopps u_char pra, *horc, *verc;
215 1.3 chopps u_short pot, count;
216 1.3 chopps short dx, dy;
217 1.18.12.1 nathanw
218 1.17 kleink port = ms->ms_portno;
219 1.10 is
220 1.3 chopps horc = ((u_char *) &count) + 1;
221 1.3 chopps verc = (u_char *) &count;
222 1.3 chopps
223 1.3 chopps /*
224 1.3 chopps * first read the three buttons.
225 1.3 chopps */
226 1.3 chopps pot = custom.potgor;
227 1.3 chopps pra = ciaa.pra;
228 1.17 kleink pot >>= port == 0 ? 8 : 12; /* contains right and middle button */
229 1.17 kleink pra >>= port == 0 ? 6 : 7; /* contains left button */
230 1.3 chopps mb = (pot & 4) / 4 + (pot & 1) * 2 + (pra & 1) * 4;
231 1.3 chopps mb ^= 0x07;
232 1.3 chopps
233 1.3 chopps /*
234 1.3 chopps * read current values of counter registers
235 1.3 chopps */
236 1.17 kleink if (port == 0)
237 1.3 chopps count = custom.joy0dat;
238 1.3 chopps else
239 1.3 chopps count = custom.joy1dat;
240 1.18.12.1 nathanw
241 1.3 chopps /*
242 1.3 chopps * take care of wraparound
243 1.3 chopps */
244 1.3 chopps dx = *horc - ms->ms_horc;
245 1.3 chopps if (dx < -127)
246 1.3 chopps dx += 255;
247 1.3 chopps else if (dx > 127)
248 1.3 chopps dx -= 255;
249 1.3 chopps dy = *verc - ms->ms_verc;
250 1.3 chopps if (dy < -127)
251 1.3 chopps dy += 255;
252 1.3 chopps else if (dy > 127)
253 1.3 chopps dy -= 255;
254 1.3 chopps
255 1.3 chopps /*
256 1.3 chopps * remember current values for next scan
257 1.3 chopps */
258 1.3 chopps ms->ms_horc = *horc;
259 1.3 chopps ms->ms_verc = *verc;
260 1.3 chopps
261 1.3 chopps ms->ms_dx = dx;
262 1.3 chopps ms->ms_dy = dy;
263 1.3 chopps ms->ms_mb = mb;
264 1.18.12.1 nathanw
265 1.3 chopps if (dx || dy || ms->ms_ub != ms->ms_mb) {
266 1.3 chopps /*
267 1.3 chopps * We have at least one event (mouse button, delta-X, or
268 1.3 chopps * delta-Y; possibly all three, and possibly three separate
269 1.3 chopps * button events). Deliver these events until we are out of
270 1.18.12.1 nathanw * changes or out of room. As events get delivered, mark them
271 1.3 chopps * `unchanged'.
272 1.3 chopps */
273 1.3 chopps any = 0;
274 1.3 chopps get = ms->ms_events.ev_get;
275 1.3 chopps put = ms->ms_events.ev_put;
276 1.3 chopps fe = &ms->ms_events.ev_q[put];
277 1.3 chopps
278 1.3 chopps mb = ms->ms_mb;
279 1.3 chopps ub = ms->ms_ub;
280 1.3 chopps while ((d = mb ^ ub) != 0) {
281 1.3 chopps /*
282 1.3 chopps * Mouse button change. Convert up to three changes
283 1.3 chopps * to the `first' change, and drop it into the event
284 1.3 chopps * queue.
285 1.3 chopps */
286 1.3 chopps if ((++put) % EV_QSIZE == get) {
287 1.3 chopps put--;
288 1.3 chopps goto out;
289 1.3 chopps }
290 1.3 chopps
291 1.3 chopps d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
292 1.3 chopps fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
293 1.3 chopps fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
294 1.3 chopps fe->time = time;
295 1.3 chopps fe++;
296 1.3 chopps
297 1.3 chopps if (put >= EV_QSIZE) {
298 1.3 chopps put = 0;
299 1.3 chopps fe = &ms->ms_events.ev_q[0];
300 1.3 chopps }
301 1.3 chopps any = 1;
302 1.3 chopps
303 1.3 chopps ub ^= d;
304 1.3 chopps }
305 1.3 chopps if (ms->ms_dx) {
306 1.3 chopps if ((++put) % EV_QSIZE == get) {
307 1.3 chopps put--;
308 1.3 chopps goto out;
309 1.3 chopps }
310 1.3 chopps
311 1.3 chopps fe->id = LOC_X_DELTA;
312 1.3 chopps fe->value = ms->ms_dx;
313 1.3 chopps fe->time = time;
314 1.4 chopps fe++;
315 1.3 chopps
316 1.3 chopps if (put >= EV_QSIZE) {
317 1.3 chopps put = 0;
318 1.3 chopps fe = &ms->ms_events.ev_q[0];
319 1.3 chopps }
320 1.3 chopps any = 1;
321 1.3 chopps
322 1.3 chopps ms->ms_dx = 0;
323 1.3 chopps }
324 1.3 chopps if (ms->ms_dy) {
325 1.3 chopps if ((++put) % EV_QSIZE == get) {
326 1.3 chopps put--;
327 1.3 chopps goto out;
328 1.3 chopps }
329 1.3 chopps
330 1.3 chopps fe->id = LOC_Y_DELTA;
331 1.3 chopps fe->value = ms->ms_dy;
332 1.3 chopps fe->time = time;
333 1.4 chopps fe++;
334 1.3 chopps
335 1.3 chopps if (put >= EV_QSIZE) {
336 1.3 chopps put = 0;
337 1.3 chopps fe = &ms->ms_events.ev_q[0];
338 1.3 chopps }
339 1.3 chopps any = 1;
340 1.1 mw
341 1.3 chopps ms->ms_dy = 0;
342 1.3 chopps }
343 1.1 mw out:
344 1.3 chopps if (any) {
345 1.3 chopps ms->ms_ub = ub;
346 1.3 chopps ms->ms_events.ev_put = put;
347 1.3 chopps EV_WAKEUP(&ms->ms_events);
348 1.3 chopps }
349 1.1 mw }
350 1.1 mw
351 1.3 chopps /*
352 1.3 chopps * reschedule handler, or if terminating,
353 1.3 chopps * handshake with ms_disable
354 1.3 chopps */
355 1.3 chopps if (ms->ms_ready)
356 1.17 kleink callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
357 1.3 chopps else
358 1.3 chopps wakeup(ms);
359 1.1 mw }
360 1.1 mw
361 1.1 mw int
362 1.18.12.1 nathanw msopen(dev_t dev, int flags, int mode, struct proc *p)
363 1.1 mw {
364 1.17 kleink struct ms_softc *sc;
365 1.17 kleink struct ms_port *ms;
366 1.17 kleink int unit, port;
367 1.3 chopps
368 1.17 kleink unit = MS_UNIT(dev);
369 1.17 kleink sc = (struct ms_softc *)getsoftc(ms_cd, unit);
370 1.3 chopps
371 1.17 kleink if (sc == NULL)
372 1.3 chopps return(EXDEV);
373 1.3 chopps
374 1.17 kleink port = MS_PORT(dev);
375 1.17 kleink ms = &sc->sc_ports[port];
376 1.17 kleink
377 1.3 chopps if (ms->ms_events.ev_io)
378 1.3 chopps return(EBUSY);
379 1.18 is
380 1.18 is /* initialize potgo bits for mouse mode */
381 1.18 is custom.potgo = custom.potgor | (0xf00 << (port * 4));
382 1.3 chopps
383 1.3 chopps ms->ms_events.ev_io = p;
384 1.3 chopps ev_init(&ms->ms_events); /* may cause sleep */
385 1.17 kleink ms_enable(ms);
386 1.3 chopps return(0);
387 1.1 mw }
388 1.1 mw
389 1.1 mw int
390 1.18.12.1 nathanw msclose(dev_t dev, int flags, int mode, struct proc *p)
391 1.1 mw {
392 1.17 kleink struct ms_port *ms;
393 1.1 mw
394 1.17 kleink ms = MS_DEV2MSPORT(dev);
395 1.3 chopps
396 1.17 kleink ms_disable(ms);
397 1.3 chopps ev_fini(&ms->ms_events);
398 1.3 chopps ms->ms_events.ev_io = NULL;
399 1.3 chopps return(0);
400 1.1 mw }
401 1.1 mw
402 1.1 mw int
403 1.18.12.1 nathanw msread(dev_t dev, struct uio *uio, int flags)
404 1.1 mw {
405 1.17 kleink struct ms_port *ms;
406 1.1 mw
407 1.17 kleink ms = MS_DEV2MSPORT(dev);
408 1.10 is
409 1.3 chopps return(ev_read(&ms->ms_events, uio, flags));
410 1.1 mw }
411 1.1 mw
412 1.1 mw int
413 1.18.12.1 nathanw msioctl(dev_t dev, u_long cmd, register caddr_t data, int flag,
414 1.18.12.1 nathanw struct proc *p)
415 1.1 mw {
416 1.17 kleink struct ms_port *ms;
417 1.1 mw
418 1.17 kleink ms = MS_DEV2MSPORT(dev);
419 1.1 mw
420 1.3 chopps switch (cmd) {
421 1.3 chopps case FIONBIO: /* we will remove this someday (soon???) */
422 1.3 chopps return(0);
423 1.3 chopps case FIOASYNC:
424 1.3 chopps ms->ms_events.ev_async = *(int *)data != 0;
425 1.3 chopps return(0);
426 1.3 chopps case TIOCSPGRP:
427 1.3 chopps if (*(int *)data != ms->ms_events.ev_io->p_pgid)
428 1.3 chopps return(EPERM);
429 1.3 chopps return(0);
430 1.3 chopps case VUIDGFORMAT: /* we only do firm_events */
431 1.3 chopps *(int *)data = VUID_FIRM_EVENT;
432 1.3 chopps return(0);
433 1.3 chopps case VUIDSFORMAT:
434 1.3 chopps if (*(int *)data != VUID_FIRM_EVENT)
435 1.3 chopps return(EINVAL);
436 1.3 chopps return(0);
437 1.3 chopps }
438 1.3 chopps return(ENOTTY);
439 1.1 mw }
440 1.1 mw
441 1.1 mw int
442 1.18.12.1 nathanw mspoll(dev_t dev, int events, struct proc *p)
443 1.1 mw {
444 1.17 kleink struct ms_port *ms;
445 1.1 mw
446 1.17 kleink ms = MS_DEV2MSPORT(dev);
447 1.10 is
448 1.12 mhitch return(ev_poll(&ms->ms_events, events, p));
449 1.18.12.4 nathanw }
450 1.18.12.4 nathanw
451 1.18.12.4 nathanw int
452 1.18.12.4 nathanw mskqfilter(dev, kn)
453 1.18.12.4 nathanw dev_t dev;
454 1.18.12.4 nathanw struct knote *kn;
455 1.18.12.4 nathanw {
456 1.18.12.4 nathanw struct ms_port *ms;
457 1.18.12.4 nathanw
458 1.18.12.4 nathanw ms = MS_DEV2MSPORT(dev);
459 1.18.12.4 nathanw
460 1.18.12.4 nathanw return (ev_kqfilter(&ms->ms_events, kn));
461 1.1 mw }
462