xref: /netbsd-src/sys/arch/arm/iomd/qms.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: qms.c,v 1.13 2007/10/17 19:53:42 garbled 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.13 2007/10/17 19:53:42 garbled 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 <machine/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 	struct device  sc_dev;
69 	struct device *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 	u_int16_t lastx;
78 	u_int16_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(struct device *, struct cfdata *, void *);
88 static void qms_attach(struct device *, struct device *, 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(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(struct device *parent, struct cfdata *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(struct device *parent, struct device *self, void *aux)
117 {
118 	struct qms_softc *sc = (void *)self;
119 	struct qms_attach_args *qa = aux;
120 	struct wsmousedev_attach_args wsmouseargs;
121 
122 	sc->sc_iot = qa->qa_iot;
123 	sc->sc_ioh = qa->qa_ioh;
124 	sc->sc_butioh = qa->qa_ioh_but;
125 
126 	/* set up wsmouse attach arguments */
127 	wsmouseargs.accessops = &qms_accessops;
128 	wsmouseargs.accesscookie = sc;
129 
130 	printf("\n");
131 
132 	sc->sc_wsmousedev =
133 	    config_found(&sc->sc_dev, &wsmouseargs, wsmousedevprint);
134 
135 	callout_init(&sc->sc_callout, 0);
136 }
137 
138 
139 static int
140 qms_enable(void *cookie)
141 {
142 	struct qms_softc *sc = cookie;
143 	int b;
144 
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;
149 
150 	/* patch up the buttons */
151 	b >>= 4;
152 	sc->lastb = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
153 
154 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
155 	return 0;
156 }
157 
158 
159 static void
160 qms_disable(void *cookie)
161 {
162 	struct qms_softc *sc = cookie;
163 
164 	callout_stop(&sc->sc_callout);
165 }
166 
167 
168 static int
169 qms_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
170 {
171 
172 	switch (cmd) {
173 	case WSMOUSEIO_GTYPE:
174 		*(int *)data = WSMOUSE_TYPE_ARCHIMEDES;
175 		return 0;
176 	}
177 
178 	return EPASSTHROUGH;
179 }
180 
181 
182 static void
183 qms_intr(void *arg)
184 {
185 	struct qms_softc *sc = arg;
186 	int b;
187 	u_int16_t x, y;
188 	int16_t dx, dy;
189 
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;
193 
194 	/* patch up the buttons */
195 	b >>= 4;
196 	b = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
197 
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,
203 				b,
204 				dx, dy, 0, 0,
205 				WSMOUSE_INPUT_DELTA);
206 
207 		/* save old values */
208 		sc->lastx = x;
209 		sc->lasty = y;
210 		sc->lastb = b;
211 	}
212 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
213 }
214 
215 
216 /* end of qms.c */
217