1 /* $NetBSD: ms.c,v 1.35 2009/01/12 03:18:26 mhitch Exp $ */
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
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
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 $");
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.
64 #include <sys/param.h>
65 #include <sys/device.h>
66 #include <sys/ioctl.h>
67 #include <sys/kernel.h>
69 #include <sys/syslog.h>
70 #include <sys/systm.h>
71 #include <sys/callout.h>
73 #include <sys/signalvar.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>
84 #include <dev/wscons/wsmousevar.h>
85 #include <dev/wscons/wsconsio.h>
88 void msattach(struct device
*, struct device
*, void *);
89 int msmatch(struct device
*, struct cfdata
*, void *);
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 */
106 struct device
*ms_wsmousedev
; /* wsmouse device */
107 int ms_wsenabled
; /* feeding events to wscons */
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
);
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)]))
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
= {
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
)
178 msattach(struct device
*pdp
, struct device
*dp
, void *auxp
)
181 struct wsmousedev_attach_args waa
;
183 struct ms_softc
*sc
= (void *) dp
;
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);
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
);
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.
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).
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.
230 ms_disable(struct ms_port
*ms
)
237 * sync with the interrupt
239 tsleep(ms
, PZERO
- 1, "mouse-disable", 0);
245 * we're emulating a mousesystems serial mouse here..
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
;
259 port
= ms
->ms_portno
;
261 horc
= ((u_char
*) &count
) + 1;
262 verc
= (u_char
*) &count
;
265 * first read the three buttons.
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;
275 * read current values of counter registers
278 count
= custom
.joy0dat
;
280 count
= custom
.joy1dat
;
283 * take care of wraparound
285 dx
= *horc
- ms
->ms_horc
;
290 dy
= *verc
- ms
->ms_verc
;
297 * remember current values for next scan
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
)
322 wsmouse_input(ms
->ms_wsmousedev
,
325 WSMOUSE_INPUT_DELTA
);
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
338 get
= ms
->ms_events
.ev_get
;
339 put
= ms
->ms_events
.ev_put
;
340 fe
= &ms
->ms_events
.ev_q
[put
];
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
350 if ((++put
) % EV_QSIZE
== get
) {
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
;
361 if (put
>= EV_QSIZE
) {
363 fe
= &ms
->ms_events
.ev_q
[0];
370 if ((++put
) % EV_QSIZE
== get
) {
375 fe
->id
= LOC_X_DELTA
;
376 fe
->value
= ms
->ms_dx
;
380 if (put
>= EV_QSIZE
) {
382 fe
= &ms
->ms_events
.ev_q
[0];
389 if ((++put
) % EV_QSIZE
== get
) {
394 fe
->id
= LOC_Y_DELTA
;
395 fe
->value
= ms
->ms_dy
;
399 if (put
>= EV_QSIZE
) {
401 fe
= &ms
->ms_events
.ev_q
[0];
410 ms
->ms_events
.ev_put
= put
;
411 EV_WAKEUP(&ms
->ms_events
);
416 * reschedule handler, or if terminating,
417 * handshake with ms_disable
420 callout_reset(&ms
->ms_intr_ch
, 2, msintr
, ms
);
426 msopen(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
433 sc
= (struct ms_softc
*)getsoftc(ms_cd
, unit
);
439 ms
= &sc
->sc_ports
[port
];
441 if (ms
->ms_events
.ev_io
)
445 /* don't allow opening when sending events to wsmouse */
446 if (ms
->ms_wsenabled
)
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 */
459 msclose(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
463 ms
= MS_DEV2MSPORT(dev
);
466 ev_fini(&ms
->ms_events
);
467 ms
->ms_events
.ev_io
= NULL
;
472 msread(dev_t dev
, struct uio
*uio
, int flags
)
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
,
487 ms
= MS_DEV2MSPORT(dev
);
490 case FIONBIO
: /* we will remove this someday (soon???) */
493 ms
->ms_events
.ev_async
= *(int *)data
!= 0;
496 if (-*(int *)data
!= ms
->ms_events
.ev_io
->p_pgid
497 && *(int *)data
!= ms
->ms_events
.ev_io
->p_pid
)
501 if (*(int *)data
!= ms
->ms_events
.ev_io
->p_pgid
)
504 case VUIDGFORMAT
: /* we only do firm_events */
505 *(int *)data
= VUID_FIRM_EVENT
;
508 if (*(int *)data
!= VUID_FIRM_EVENT
)
516 mspoll(dev_t dev
, int events
, struct lwp
*l
)
520 ms
= MS_DEV2MSPORT(dev
);
522 return(ev_poll(&ms
->ms_events
, events
, l
));
526 mskqfilter(dev_t dev
, struct knote
*kn
)
530 ms
= MS_DEV2MSPORT(dev
);
532 return (ev_kqfilter(&ms
->ms_events
, kn
));
538 ms_wscons_ioctl(void *cookie
, u_long cmd
, void *data
, int flag
,
542 case WSMOUSEIO_GTYPE
:
543 *(u_int
*)data
= WSMOUSE_TYPE_AMIGA
;
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
)
559 port
->ms_wsenabled
= 1;
566 ms_wscons_disable(void *cookie
)
568 struct ms_port
*port
= cookie
;
570 if (port
->ms_wsenabled
)
572 port
->ms_wsenabled
= 0;