Fix up mix of man(7)/mdoc(7).
[netbsd-mini2440.git] / sys / arch / arm / iomd / qms.c
blobed2e5fed1a80809cb70f3c4e38bae2d2b2280790
1 /* $NetBSD: qms.c,v 1.13 2007/10/17 19:53:42 garbled Exp $ */
3 /*-
4 * Copyright (c) 2001 Reinoud Zandijk
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Reinoud Zandijk
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 * Quadrature mouse driver for the wscons as used in the IOMD
42 #include <sys/param.h>
44 __KERNEL_RCSID(0, "$NetBSD: qms.c,v 1.13 2007/10/17 19:53:42 garbled Exp $");
46 #include <sys/callout.h>
47 #include <sys/device.h>
48 #include <sys/errno.h>
49 #include <sys/ioctl.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/proc.h>
53 #include <sys/tty.h>
54 #include <sys/types.h>
55 #include <sys/syslog.h>
56 #include <sys/systm.h>
57 #include <sys/select.h>
59 #include <machine/bus.h>
60 #include <machine/intr.h>
62 #include <arm/iomd/iomdvar.h>
64 #include <dev/wscons/wsconsio.h>
65 #include <dev/wscons/wsmousevar.h>
67 struct qms_softc {
68 struct device sc_dev;
69 struct device *sc_wsmousedev;
71 bus_space_tag_t sc_iot; /* bus tag */
72 bus_space_handle_t sc_ioh; /* bus handle for XY */
73 bus_space_handle_t sc_butioh; /* bus handle for buttons */
75 struct callout sc_callout;
77 u_int16_t lastx;
78 u_int16_t lasty;
79 int lastb;
82 /* Offsets of hardware registers */
83 #define QMS_MOUSEX 0 /* 16 bits X register */
84 #define QMS_MOUSEY 1 /* 16 bits Y register */
85 #define QMS_BUTTONS 0 /* mouse buttons in bits 4,5,6 */
87 static int qms_match(struct device *, struct cfdata *, void *);
88 static void qms_attach(struct device *, struct device *, void *);
90 static int qms_enable(void *);
91 static int qms_ioctl(void *, u_long, void *, int, struct lwp *);
92 static void qms_disable(void *cookie);
93 static void qms_intr(void *arg);
95 CFATTACH_DECL(qms, sizeof(struct qms_softc),
96 qms_match, qms_attach, NULL, NULL);
98 static struct wsmouse_accessops qms_accessops = {
99 qms_enable, qms_ioctl, qms_disable
103 static int
104 qms_match(struct device *parent, struct cfdata *cf, void *aux)
106 struct qms_attach_args *qa = aux;
108 if (strcmp(qa->qa_name, "qms") == 0)
109 return 1;
111 return 0;
115 static void
116 qms_attach(struct device *parent, struct device *self, void *aux)
118 struct qms_softc *sc = (void *)self;
119 struct qms_attach_args *qa = aux;
120 struct wsmousedev_attach_args wsmouseargs;
122 sc->sc_iot = qa->qa_iot;
123 sc->sc_ioh = qa->qa_ioh;
124 sc->sc_butioh = qa->qa_ioh_but;
126 /* set up wsmouse attach arguments */
127 wsmouseargs.accessops = &qms_accessops;
128 wsmouseargs.accesscookie = sc;
130 printf("\n");
132 sc->sc_wsmousedev =
133 config_found(&sc->sc_dev, &wsmouseargs, wsmousedevprint);
135 callout_init(&sc->sc_callout, 0);
139 static int
140 qms_enable(void *cookie)
142 struct qms_softc *sc = cookie;
143 int b;
145 /* We don't want the mouse to warp on open. */
146 sc->lastx = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
147 sc->lasty = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
148 b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
150 /* patch up the buttons */
151 b >>= 4;
152 sc->lastb = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
154 callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
155 return 0;
159 static void
160 qms_disable(void *cookie)
162 struct qms_softc *sc = cookie;
164 callout_stop(&sc->sc_callout);
168 static int
169 qms_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
172 switch (cmd) {
173 case WSMOUSEIO_GTYPE:
174 *(int *)data = WSMOUSE_TYPE_ARCHIMEDES;
175 return 0;
178 return EPASSTHROUGH;
182 static void
183 qms_intr(void *arg)
185 struct qms_softc *sc = arg;
186 int b;
187 u_int16_t x, y;
188 int16_t dx, dy;
190 x = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
191 y = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
192 b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
194 /* patch up the buttons */
195 b >>= 4;
196 b = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
198 if ((x != sc->lastx) || (y != sc->lasty) || (b != sc->lastb)) {
199 /* This assumes that int16_t is two's complement. */
200 dx = x - sc->lastx;
201 dy = y - sc->lasty;
202 wsmouse_input(sc->sc_wsmousedev,
204 dx, dy, 0, 0,
205 WSMOUSE_INPUT_DELTA);
207 /* save old values */
208 sc->lastx = x;
209 sc->lasty = y;
210 sc->lastb = b;
212 callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
216 /* end of qms.c */