Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / amiga / dev / ms.c
blob9e244a4d30ab25e9e2ff9c8140b4d717df930841
1 /* $NetBSD: ms.c,v 1.35 2009/01/12 03:18:26 mhitch Exp $ */
3 /*
4 * based on:
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This software was developed by the Computer Systems Engineering group
10 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11 * contributed to Berkeley.
13 * All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Lawrence Berkeley Laboratory.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
42 * @(#)ms.c 8.1 (Berkeley) 6/11/93
44 * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp (LBL)
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.35 2009/01/12 03:18:26 mhitch Exp $");
51 * Mouse driver.
53 * wscons aware. Attaches two wsmouse devices, one for each port.
54 * Also still exports its own device entry points so it is possible
55 * to open this and read firm_events.
56 * The events go only to one place at a time:
57 * - When somebody has opened a ms device directly wsmouse cannot be activated.
58 * (when wsmouse is opened it calls ms_enable to activate)
59 * - When feeding events to wsmouse open of ms device will fail.
62 #include "wsmouse.h"
64 #include <sys/param.h>
65 #include <sys/device.h>
66 #include <sys/ioctl.h>
67 #include <sys/kernel.h>
68 #include <sys/proc.h>
69 #include <sys/syslog.h>
70 #include <sys/systm.h>
71 #include <sys/callout.h>
72 #include <sys/tty.h>
73 #include <sys/signalvar.h>
74 #include <sys/conf.h>
76 #include <amiga/dev/event_var.h>
77 #include <amiga/dev/vuid_event.h>
79 #include <amiga/amiga/custom.h>
80 #include <amiga/amiga/cia.h>
81 #include <amiga/amiga/device.h>
83 #if NWSMOUSE > 0
84 #include <dev/wscons/wsmousevar.h>
85 #include <dev/wscons/wsconsio.h>
86 #endif
88 void msattach(struct device *, struct device *, void *);
89 int msmatch(struct device *, struct cfdata *, void *);
91 /* per-port state */
92 struct ms_port {
93 int ms_portno; /* which hardware port, for msintr() */
95 struct callout ms_intr_ch;
97 u_char ms_horc; /* horizontal counter on last scan */
98 u_char ms_verc; /* vertical counter on last scan */
99 char ms_mb; /* mouse button state */
100 char ms_ub; /* user button state */
101 int ms_dx; /* delta-x */
102 int ms_dy; /* delta-y */
103 volatile int ms_ready; /* event queue is ready */
104 struct evvar ms_events; /* event queue state */
105 #if NWSMOUSE > 0
106 struct device *ms_wsmousedev; /* wsmouse device */
107 int ms_wsenabled; /* feeding events to wscons */
108 #endif
111 #define MS_NPORTS 2
113 struct ms_softc {
114 struct device sc_dev; /* base device */
115 struct ms_port sc_ports[MS_NPORTS];
118 CFATTACH_DECL(ms, sizeof(struct ms_softc),
119 msmatch, msattach, NULL, NULL);
121 void msintr(void *);
122 void ms_enable(struct ms_port *);
123 void ms_disable(struct ms_port *);
125 extern struct cfdriver ms_cd;
127 dev_type_open(msopen);
128 dev_type_close(msclose);
129 dev_type_read(msread);
130 dev_type_ioctl(msioctl);
131 dev_type_poll(mspoll);
132 dev_type_kqfilter(mskqfilter);
134 const struct cdevsw ms_cdevsw = {
135 msopen, msclose, msread, nowrite, msioctl,
136 nostop, notty, mspoll, nommap, mskqfilter,
139 #define MS_UNIT(d) ((minor(d) & ~0x1) >> 1)
140 #define MS_PORT(d) (minor(d) & 0x1)
143 * Given a dev_t, return a pointer to the port's hardware state.
144 * Assumes the unit to be valid, so do *not* use this in msopen().
146 #define MS_DEV2MSPORT(d) \
147 (&(((struct ms_softc *)getsoftc(ms_cd, MS_UNIT(d)))->sc_ports[MS_PORT(d)]))
149 #if NWSMOUSE > 0
151 * Callbacks for wscons.
153 static int ms_wscons_enable(void *);
154 static int ms_wscons_ioctl(void *, u_long, void *, int, struct lwp *);
155 static void ms_wscons_disable(void *);
157 static struct wsmouse_accessops ms_wscons_accessops = {
158 ms_wscons_enable,
159 ms_wscons_ioctl,
160 ms_wscons_disable
162 #endif
165 msmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
167 static int ms_matched = 0;
169 /* Allow only one instance. */
170 if (!matchname((char *)auxp, "ms") || ms_matched)
171 return 0;
173 ms_matched = 1;
174 return 1;
177 void
178 msattach(struct device *pdp, struct device *dp, void *auxp)
180 #if NWSMOUSE > 0
181 struct wsmousedev_attach_args waa;
182 #endif
183 struct ms_softc *sc = (void *) dp;
184 int i;
186 printf("\n");
187 for (i = 0; i < MS_NPORTS; i++) {
188 sc->sc_ports[i].ms_portno = i;
189 callout_init(&sc->sc_ports[i].ms_intr_ch, 0);
190 #if NWSMOUSE > 0
191 waa.accessops = &ms_wscons_accessops;
192 waa.accesscookie = &sc->sc_ports[i];
194 sc->sc_ports[i].ms_wsenabled = 0;
195 sc->sc_ports[i].ms_wsmousedev =
196 config_found(dp, &waa, wsmousedevprint);
197 #endif
202 * Amiga mice are hooked up to one of the two "game" ports, where
203 * the main mouse is usually on the first port, and port 2 can
204 * be used by a joystick. Nevertheless, we support two mouse
205 * devices, /dev/mouse0 and /dev/mouse1 (with a link of /dev/mouse to
206 * the device that represents the port of the mouse in use).
210 * enable scanner, called when someone opens the port.
212 void
213 ms_enable(struct ms_port *ms)
217 * use this as flag to the "interrupt" to tell it when to
218 * shut off (when it's reset to 0).
220 ms->ms_ready = 1;
222 callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
226 * disable scanner. Just set ms_ready to 0, and after the next
227 * timeout taken, no further timeouts will be initiated.
229 void
230 ms_disable(struct ms_port *ms)
232 int s;
234 s = splhigh ();
235 ms->ms_ready = 0;
237 * sync with the interrupt
239 tsleep(ms, PZERO - 1, "mouse-disable", 0);
240 splx(s);
245 * we're emulating a mousesystems serial mouse here..
247 void
248 msintr(void *arg)
250 static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
251 static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
252 struct ms_port *ms = arg;
253 struct firm_event *fe;
254 int mb, ub, d, get, put, any, port;
255 u_char pra, *horc, *verc;
256 u_short pot, count;
257 short dx, dy;
259 port = ms->ms_portno;
261 horc = ((u_char *) &count) + 1;
262 verc = (u_char *) &count;
265 * first read the three buttons.
267 pot = custom.potgor;
268 pra = ciaa.pra;
269 pot >>= port == 0 ? 8 : 12; /* contains right and middle button */
270 pra >>= port == 0 ? 6 : 7; /* contains left button */
271 mb = (pot & 4) / 4 + (pot & 1) * 2 + (pra & 1) * 4;
272 mb ^= 0x07;
275 * read current values of counter registers
277 if (port == 0)
278 count = custom.joy0dat;
279 else
280 count = custom.joy1dat;
283 * take care of wraparound
285 dx = *horc - ms->ms_horc;
286 if (dx < -127)
287 dx += 255;
288 else if (dx > 127)
289 dx -= 255;
290 dy = *verc - ms->ms_verc;
291 if (dy < -127)
292 dy += 255;
293 else if (dy > 127)
294 dy -= 255;
297 * remember current values for next scan
299 ms->ms_horc = *horc;
300 ms->ms_verc = *verc;
302 ms->ms_dx = dx;
303 ms->ms_dy = dy;
304 ms->ms_mb = mb;
306 #if NWSMOUSE > 0
308 * If we have attached wsmouse and we are not opened
309 * directly then pass events to wscons.
311 if (ms->ms_wsmousedev && ms->ms_wsenabled)
313 int buttons = 0;
315 if (mb & 4)
316 buttons |= 1;
317 if (mb & 2)
318 buttons |= 2;
319 if (mb & 1)
320 buttons |= 4;
322 wsmouse_input(ms->ms_wsmousedev,
323 buttons,
324 dx, -dy, 0, 0,
325 WSMOUSE_INPUT_DELTA);
327 } else
328 #endif
329 if (dx || dy || ms->ms_ub != ms->ms_mb) {
331 * We have at least one event (mouse button, delta-X, or
332 * delta-Y; possibly all three, and possibly three separate
333 * button events). Deliver these events until we are out of
334 * changes or out of room. As events get delivered, mark them
335 * `unchanged'.
337 any = 0;
338 get = ms->ms_events.ev_get;
339 put = ms->ms_events.ev_put;
340 fe = &ms->ms_events.ev_q[put];
342 mb = ms->ms_mb;
343 ub = ms->ms_ub;
344 while ((d = mb ^ ub) != 0) {
346 * Mouse button change. Convert up to three changes
347 * to the `first' change, and drop it into the event
348 * queue.
350 if ((++put) % EV_QSIZE == get) {
351 put--;
352 goto out;
355 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
356 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
357 fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
358 firm_gettime(fe);
359 fe++;
361 if (put >= EV_QSIZE) {
362 put = 0;
363 fe = &ms->ms_events.ev_q[0];
365 any = 1;
367 ub ^= d;
369 if (ms->ms_dx) {
370 if ((++put) % EV_QSIZE == get) {
371 put--;
372 goto out;
375 fe->id = LOC_X_DELTA;
376 fe->value = ms->ms_dx;
377 firm_gettime(fe);
378 fe++;
380 if (put >= EV_QSIZE) {
381 put = 0;
382 fe = &ms->ms_events.ev_q[0];
384 any = 1;
386 ms->ms_dx = 0;
388 if (ms->ms_dy) {
389 if ((++put) % EV_QSIZE == get) {
390 put--;
391 goto out;
394 fe->id = LOC_Y_DELTA;
395 fe->value = ms->ms_dy;
396 firm_gettime(fe);
397 fe++;
399 if (put >= EV_QSIZE) {
400 put = 0;
401 fe = &ms->ms_events.ev_q[0];
403 any = 1;
405 ms->ms_dy = 0;
407 out:
408 if (any) {
409 ms->ms_ub = ub;
410 ms->ms_events.ev_put = put;
411 EV_WAKEUP(&ms->ms_events);
416 * reschedule handler, or if terminating,
417 * handshake with ms_disable
419 if (ms->ms_ready)
420 callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
421 else
422 wakeup(ms);
426 msopen(dev_t dev, int flags, int mode, struct lwp *l)
428 struct ms_softc *sc;
429 struct ms_port *ms;
430 int unit, port;
432 unit = MS_UNIT(dev);
433 sc = (struct ms_softc *)getsoftc(ms_cd, unit);
435 if (sc == NULL)
436 return(EXDEV);
438 port = MS_PORT(dev);
439 ms = &sc->sc_ports[port];
441 if (ms->ms_events.ev_io)
442 return(EBUSY);
444 #if NWSMOUSE > 0
445 /* don't allow opening when sending events to wsmouse */
446 if (ms->ms_wsenabled)
447 return EBUSY;
448 #endif
449 /* initialize potgo bits for mouse mode */
450 custom.potgo = custom.potgor | (0xf00 << (port * 4));
452 ms->ms_events.ev_io = l->l_proc;
453 ev_init(&ms->ms_events); /* may cause sleep */
454 ms_enable(ms);
455 return(0);
459 msclose(dev_t dev, int flags, int mode, struct lwp *l)
461 struct ms_port *ms;
463 ms = MS_DEV2MSPORT(dev);
465 ms_disable(ms);
466 ev_fini(&ms->ms_events);
467 ms->ms_events.ev_io = NULL;
468 return(0);
472 msread(dev_t dev, struct uio *uio, int flags)
474 struct ms_port *ms;
476 ms = MS_DEV2MSPORT(dev);
478 return(ev_read(&ms->ms_events, uio, flags));
482 msioctl(dev_t dev, u_long cmd, register void *data, int flag,
483 struct lwp *l)
485 struct ms_port *ms;
487 ms = MS_DEV2MSPORT(dev);
489 switch (cmd) {
490 case FIONBIO: /* we will remove this someday (soon???) */
491 return(0);
492 case FIOASYNC:
493 ms->ms_events.ev_async = *(int *)data != 0;
494 return(0);
495 case FIOSETOWN:
496 if (-*(int *)data != ms->ms_events.ev_io->p_pgid
497 && *(int *)data != ms->ms_events.ev_io->p_pid)
498 return(EPERM);
499 return(0);
500 case TIOCSPGRP:
501 if (*(int *)data != ms->ms_events.ev_io->p_pgid)
502 return(EPERM);
503 return(0);
504 case VUIDGFORMAT: /* we only do firm_events */
505 *(int *)data = VUID_FIRM_EVENT;
506 return(0);
507 case VUIDSFORMAT:
508 if (*(int *)data != VUID_FIRM_EVENT)
509 return(EINVAL);
510 return(0);
512 return(ENOTTY);
516 mspoll(dev_t dev, int events, struct lwp *l)
518 struct ms_port *ms;
520 ms = MS_DEV2MSPORT(dev);
522 return(ev_poll(&ms->ms_events, events, l));
526 mskqfilter(dev_t dev, struct knote *kn)
528 struct ms_port *ms;
530 ms = MS_DEV2MSPORT(dev);
532 return (ev_kqfilter(&ms->ms_events, kn));
535 #if NWSMOUSE > 0
537 static int
538 ms_wscons_ioctl(void *cookie, u_long cmd, void *data, int flag,
539 struct lwp *l)
541 switch(cmd) {
542 case WSMOUSEIO_GTYPE:
543 *(u_int*)data = WSMOUSE_TYPE_AMIGA;
544 return (0);
547 return -1;
550 static int
551 ms_wscons_enable(void *cookie)
553 struct ms_port *port = cookie;
555 /* somebody reading events from us directly? */
556 if (port->ms_events.ev_io)
557 return EBUSY;
559 port->ms_wsenabled = 1;
560 ms_enable(port);
562 return 0;
565 static void
566 ms_wscons_disable(void *cookie)
568 struct ms_port *port = cookie;
570 if (port->ms_wsenabled)
571 ms_disable(port);
572 port->ms_wsenabled = 0;
575 #endif