xref: /netbsd-src/sys/arch/arm/iomd/qms.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: qms.c,v 1.18 2012/05/14 10:38:08 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 Reinoud Zandijk
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Reinoud Zandijk
9  *
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.
25  *
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.
37  */
38 /*
39  * Quadrature mouse driver for the wscons as used in the IOMD
40  */
41 
42 #include <sys/param.h>
43 
44 __KERNEL_RCSID(0, "$NetBSD: qms.c,v 1.18 2012/05/14 10:38:08 skrll Exp $");
45 
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>
58 
59 #include <sys/bus.h>
60 #include <machine/intr.h>
61 
62 #include <arm/iomd/iomdvar.h>
63 
64 #include <dev/wscons/wsconsio.h>
65 #include <dev/wscons/wsmousevar.h>
66 
67 struct qms_softc {
68 	device_t sc_dev;
69 	device_t sc_wsmousedev;
70 
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 */
74 
75 	struct callout sc_callout;
76 
77 	uint16_t lastx;
78 	uint16_t lasty;
79 	int lastb;
80 };
81 
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 */
86 
87 static int  qms_match(device_t , cfdata_t , void *);
88 static void qms_attach(device_t , device_t , void *);
89 
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);
94 
95 CFATTACH_DECL_NEW(qms, sizeof(struct qms_softc),
96     qms_match, qms_attach, NULL, NULL);
97 
98 static struct wsmouse_accessops qms_accessops = {
99 	qms_enable, qms_ioctl, qms_disable
100 };
101 
102 
103 static int
104 qms_match(device_t parent, cfdata_t cf, void *aux)
105 {
106 	struct qms_attach_args *qa = aux;
107 
108 	if (strcmp(qa->qa_name, "qms") == 0)
109 		return 1;
110 
111 	return 0;
112 }
113 
114 
115 static void
116 qms_attach(device_t parent, device_t self, void *aux)
117 {
118 	struct qms_softc *sc = device_private(self);
119 	struct qms_attach_args *qa = aux;
120 	struct wsmousedev_attach_args wsmouseargs;
121 
122 	sc->sc_dev = self;
123 	sc->sc_iot = qa->qa_iot;
124 	sc->sc_ioh = qa->qa_ioh;
125 	sc->sc_butioh = qa->qa_ioh_but;
126 
127 	/* set up wsmouse attach arguments */
128 	wsmouseargs.accessops = &qms_accessops;
129 	wsmouseargs.accesscookie = sc;
130 
131 	aprint_normal("\n");
132 
133 	sc->sc_wsmousedev =
134 	    config_found(sc->sc_dev, &wsmouseargs, wsmousedevprint);
135 
136 	callout_init(&sc->sc_callout, 0);
137 }
138 
139 
140 static int
141 qms_enable(void *cookie)
142 {
143 	struct qms_softc *sc = cookie;
144 	int b;
145 
146 	/* We don't want the mouse to warp on open. */
147 	sc->lastx = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
148 	sc->lasty = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
149 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
150 
151 	/* patch up the buttons */
152 	b >>= 4;
153 	sc->lastb = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
154 
155 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
156 	return 0;
157 }
158 
159 
160 static void
161 qms_disable(void *cookie)
162 {
163 	struct qms_softc *sc = cookie;
164 
165 	callout_stop(&sc->sc_callout);
166 }
167 
168 
169 static int
170 qms_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
171 {
172 
173 	switch (cmd) {
174 	case WSMOUSEIO_GTYPE:
175 		*(int *)data = WSMOUSE_TYPE_ARCHIMEDES;
176 		return 0;
177 	}
178 
179 	return EPASSTHROUGH;
180 }
181 
182 
183 static void
184 qms_intr(void *arg)
185 {
186 	struct qms_softc *sc = arg;
187 	int b;
188 	uint16_t x, y;
189 	int16_t dx, dy;
190 
191 	x = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
192 	y = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
193 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
194 
195 	/* patch up the buttons */
196 	b >>= 4;
197 	b = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
198 
199 	if ((x != sc->lastx) || (y != sc->lasty) || (b != sc->lastb)) {
200 		/* This assumes that int16_t is two's complement. */
201 		dx = x - sc->lastx;
202 		dy = y - sc->lasty;
203 		wsmouse_input(sc->sc_wsmousedev,
204 				b,
205 				dx, dy, 0, 0,
206 				WSMOUSE_INPUT_DELTA);
207 
208 		/* save old values */
209 		sc->lastx = x;
210 		sc->lasty = y;
211 		sc->lastb = b;
212 	}
213 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
214 }
215 
216 
217 /* end of qms.c */
218