qms.c revision 1.18 1 1.18 skrll /* $NetBSD: qms.c,v 1.18 2012/05/14 10:38:08 skrll Exp $ */
2 1.1 reinoud
3 1.7 bjh21 /*-
4 1.7 bjh21 * Copyright (c) 2001 Reinoud Zandijk
5 1.7 bjh21 * All rights reserved.
6 1.7 bjh21 *
7 1.7 bjh21 * This code is derived from software contributed to The NetBSD Foundation
8 1.7 bjh21 * by Reinoud Zandijk
9 1.1 reinoud *
10 1.1 reinoud * Redistribution and use in source and binary forms, with or without
11 1.1 reinoud * modification, are permitted provided that the following conditions
12 1.1 reinoud * are met:
13 1.1 reinoud * 1. Redistributions of source code must retain the above copyright
14 1.1 reinoud * notice, this list of conditions and the following disclaimer.
15 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 reinoud * notice, this list of conditions and the following disclaimer in the
17 1.1 reinoud * documentation and/or other materials provided with the distribution.
18 1.15 martin * 3. All advertising materials mentioning features or use of this software
19 1.15 martin * must display the following acknowledgement:
20 1.15 martin * This product includes software developed by the NetBSD
21 1.15 martin * Foundation, Inc. and its contributors.
22 1.15 martin * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.15 martin * contributors may be used to endorse or promote products derived
24 1.15 martin * from this software without specific prior written permission.
25 1.1 reinoud *
26 1.7 bjh21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.7 bjh21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.7 bjh21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.7 bjh21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.7 bjh21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.7 bjh21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.7 bjh21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.7 bjh21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.7 bjh21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.7 bjh21 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.7 bjh21 * POSSIBILITY OF SUCH DAMAGE.
37 1.1 reinoud */
38 1.1 reinoud /*
39 1.7 bjh21 * Quadrature mouse driver for the wscons as used in the IOMD
40 1.1 reinoud */
41 1.5 lukem
42 1.7 bjh21 #include <sys/param.h>
43 1.7 bjh21
44 1.18 skrll __KERNEL_RCSID(0, "$NetBSD: qms.c,v 1.18 2012/05/14 10:38:08 skrll Exp $");
45 1.1 reinoud
46 1.7 bjh21 #include <sys/callout.h>
47 1.7 bjh21 #include <sys/device.h>
48 1.7 bjh21 #include <sys/errno.h>
49 1.1 reinoud #include <sys/ioctl.h>
50 1.7 bjh21 #include <sys/kernel.h>
51 1.7 bjh21 #include <sys/malloc.h>
52 1.7 bjh21 #include <sys/proc.h>
53 1.1 reinoud #include <sys/tty.h>
54 1.1 reinoud #include <sys/types.h>
55 1.7 bjh21 #include <sys/syslog.h>
56 1.7 bjh21 #include <sys/systm.h>
57 1.7 bjh21 #include <sys/select.h>
58 1.1 reinoud
59 1.16 dyoung #include <sys/bus.h>
60 1.7 bjh21 #include <machine/intr.h>
61 1.1 reinoud
62 1.7 bjh21 #include <arm/iomd/iomdvar.h>
63 1.1 reinoud
64 1.7 bjh21 #include <dev/wscons/wsconsio.h>
65 1.7 bjh21 #include <dev/wscons/wsmousevar.h>
66 1.1 reinoud
67 1.7 bjh21 struct qms_softc {
68 1.18 skrll device_t sc_dev;
69 1.18 skrll device_t sc_wsmousedev;
70 1.1 reinoud
71 1.7 bjh21 bus_space_tag_t sc_iot; /* bus tag */
72 1.7 bjh21 bus_space_handle_t sc_ioh; /* bus handle for XY */
73 1.7 bjh21 bus_space_handle_t sc_butioh; /* bus handle for buttons */
74 1.2 gehenna
75 1.7 bjh21 struct callout sc_callout;
76 1.2 gehenna
77 1.17 skrll uint16_t lastx;
78 1.17 skrll uint16_t lasty;
79 1.7 bjh21 int lastb;
80 1.2 gehenna };
81 1.1 reinoud
82 1.1 reinoud /* Offsets of hardware registers */
83 1.7 bjh21 #define QMS_MOUSEX 0 /* 16 bits X register */
84 1.7 bjh21 #define QMS_MOUSEY 1 /* 16 bits Y register */
85 1.7 bjh21 #define QMS_BUTTONS 0 /* mouse buttons in bits 4,5,6 */
86 1.7 bjh21
87 1.18 skrll static int qms_match(device_t , cfdata_t , void *);
88 1.18 skrll static void qms_attach(device_t , device_t , void *);
89 1.7 bjh21
90 1.7 bjh21 static int qms_enable(void *);
91 1.11 christos static int qms_ioctl(void *, u_long, void *, int, struct lwp *);
92 1.7 bjh21 static void qms_disable(void *cookie);
93 1.7 bjh21 static void qms_intr(void *arg);
94 1.1 reinoud
95 1.18 skrll CFATTACH_DECL_NEW(qms, sizeof(struct qms_softc),
96 1.7 bjh21 qms_match, qms_attach, NULL, NULL);
97 1.1 reinoud
98 1.7 bjh21 static struct wsmouse_accessops qms_accessops = {
99 1.7 bjh21 qms_enable, qms_ioctl, qms_disable
100 1.7 bjh21 };
101 1.1 reinoud
102 1.1 reinoud
103 1.7 bjh21 static int
104 1.18 skrll qms_match(device_t parent, cfdata_t cf, void *aux)
105 1.1 reinoud {
106 1.7 bjh21 struct qms_attach_args *qa = aux;
107 1.1 reinoud
108 1.7 bjh21 if (strcmp(qa->qa_name, "qms") == 0)
109 1.9 bjh21 return 1;
110 1.1 reinoud
111 1.9 bjh21 return 0;
112 1.1 reinoud }
113 1.1 reinoud
114 1.1 reinoud
115 1.7 bjh21 static void
116 1.18 skrll qms_attach(device_t parent, device_t self, void *aux)
117 1.1 reinoud {
118 1.18 skrll struct qms_softc *sc = device_private(self);
119 1.7 bjh21 struct qms_attach_args *qa = aux;
120 1.7 bjh21 struct wsmousedev_attach_args wsmouseargs;
121 1.1 reinoud
122 1.18 skrll sc->sc_dev = self;
123 1.7 bjh21 sc->sc_iot = qa->qa_iot;
124 1.7 bjh21 sc->sc_ioh = qa->qa_ioh;
125 1.7 bjh21 sc->sc_butioh = qa->qa_ioh_but;
126 1.1 reinoud
127 1.7 bjh21 /* set up wsmouse attach arguments */
128 1.7 bjh21 wsmouseargs.accessops = &qms_accessops;
129 1.7 bjh21 wsmouseargs.accesscookie = sc;
130 1.1 reinoud
131 1.18 skrll aprint_normal("\n");
132 1.1 reinoud
133 1.7 bjh21 sc->sc_wsmousedev =
134 1.18 skrll config_found(sc->sc_dev, &wsmouseargs, wsmousedevprint);
135 1.1 reinoud
136 1.12 ad callout_init(&sc->sc_callout, 0);
137 1.1 reinoud }
138 1.1 reinoud
139 1.1 reinoud
140 1.7 bjh21 static int
141 1.7 bjh21 qms_enable(void *cookie)
142 1.1 reinoud {
143 1.7 bjh21 struct qms_softc *sc = cookie;
144 1.7 bjh21 int b;
145 1.1 reinoud
146 1.7 bjh21 /* We don't want the mouse to warp on open. */
147 1.7 bjh21 sc->lastx = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
148 1.7 bjh21 sc->lasty = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
149 1.7 bjh21 b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
150 1.1 reinoud
151 1.7 bjh21 /* patch up the buttons */
152 1.1 reinoud b >>= 4;
153 1.7 bjh21 sc->lastb = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
154 1.1 reinoud
155 1.7 bjh21 callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
156 1.7 bjh21 return 0;
157 1.1 reinoud }
158 1.1 reinoud
159 1.1 reinoud
160 1.7 bjh21 static void
161 1.7 bjh21 qms_disable(void *cookie)
162 1.1 reinoud {
163 1.7 bjh21 struct qms_softc *sc = cookie;
164 1.1 reinoud
165 1.7 bjh21 callout_stop(&sc->sc_callout);
166 1.1 reinoud }
167 1.1 reinoud
168 1.1 reinoud
169 1.7 bjh21 static int
170 1.11 christos qms_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
171 1.1 reinoud {
172 1.1 reinoud
173 1.7 bjh21 switch (cmd) {
174 1.7 bjh21 case WSMOUSEIO_GTYPE:
175 1.7 bjh21 *(int *)data = WSMOUSE_TYPE_ARCHIMEDES;
176 1.7 bjh21 return 0;
177 1.1 reinoud }
178 1.1 reinoud
179 1.7 bjh21 return EPASSTHROUGH;
180 1.1 reinoud }
181 1.3 jdolecek
182 1.7 bjh21
183 1.3 jdolecek static void
184 1.7 bjh21 qms_intr(void *arg)
185 1.3 jdolecek {
186 1.7 bjh21 struct qms_softc *sc = arg;
187 1.7 bjh21 int b;
188 1.17 skrll uint16_t x, y;
189 1.7 bjh21 int16_t dx, dy;
190 1.7 bjh21
191 1.7 bjh21 x = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
192 1.7 bjh21 y = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
193 1.7 bjh21 b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
194 1.3 jdolecek
195 1.7 bjh21 /* patch up the buttons */
196 1.7 bjh21 b >>= 4;
197 1.7 bjh21 b = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
198 1.3 jdolecek
199 1.7 bjh21 if ((x != sc->lastx) || (y != sc->lasty) || (b != sc->lastb)) {
200 1.7 bjh21 /* This assumes that int16_t is two's complement. */
201 1.7 bjh21 dx = x - sc->lastx;
202 1.7 bjh21 dy = y - sc->lasty;
203 1.10 plunky wsmouse_input(sc->sc_wsmousedev,
204 1.10 plunky b,
205 1.10 plunky dx, dy, 0, 0,
206 1.10 plunky WSMOUSE_INPUT_DELTA);
207 1.3 jdolecek
208 1.7 bjh21 /* save old values */
209 1.7 bjh21 sc->lastx = x;
210 1.7 bjh21 sc->lasty = y;
211 1.7 bjh21 sc->lastb = b;
212 1.9 bjh21 }
213 1.7 bjh21 callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
214 1.3 jdolecek }
215 1.3 jdolecek
216 1.1 reinoud
217 1.7 bjh21 /* end of qms.c */
218