xref: /netbsd-src/sys/arch/arm/iomd/qms.c (revision 122b5006ee1bd67145794b4cde92f4fe4781a5ec)
1 /*	$NetBSD: qms.c,v 1.21 2021/08/07 16:18:44 thorpej 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*
32  * Quadrature mouse driver for the wscons as used in the IOMD
33  */
34 
35 #include <sys/param.h>
36 
37 __KERNEL_RCSID(0, "$NetBSD: qms.c,v 1.21 2021/08/07 16:18:44 thorpej Exp $");
38 
39 #include <sys/callout.h>
40 #include <sys/device.h>
41 #include <sys/errno.h>
42 #include <sys/ioctl.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/tty.h>
47 #include <sys/types.h>
48 #include <sys/syslog.h>
49 #include <sys/systm.h>
50 #include <sys/select.h>
51 
52 #include <sys/bus.h>
53 #include <machine/intr.h>
54 
55 #include <arm/iomd/iomdvar.h>
56 
57 #include <dev/wscons/wsconsio.h>
58 #include <dev/wscons/wsmousevar.h>
59 
60 struct qms_softc {
61 	device_t sc_dev;
62 	device_t sc_wsmousedev;
63 
64 	bus_space_tag_t sc_iot;		/* bus tag */
65 	bus_space_handle_t sc_ioh;	/* bus handle for XY */
66 	bus_space_handle_t sc_butioh;	/* bus handle for buttons */
67 
68 	struct callout sc_callout;
69 
70 	uint16_t lastx;
71 	uint16_t lasty;
72 	int lastb;
73 };
74 
75 /* Offsets of hardware registers */
76 #define QMS_MOUSEX	0		/* 16 bits X register */
77 #define QMS_MOUSEY	1		/* 16 bits Y register */
78 #define QMS_BUTTONS	0 		/* mouse buttons in bits 4,5,6 */
79 
80 static int  qms_match(device_t , cfdata_t , void *);
81 static void qms_attach(device_t , device_t , void *);
82 
83 static int qms_enable(void *);
84 static int qms_ioctl(void *, u_long, void *, int, struct lwp *);
85 static void qms_disable(void *cookie);
86 static void qms_intr(void *arg);
87 
88 CFATTACH_DECL_NEW(qms, sizeof(struct qms_softc),
89     qms_match, qms_attach, NULL, NULL);
90 
91 static struct wsmouse_accessops qms_accessops = {
92 	qms_enable, qms_ioctl, qms_disable
93 };
94 
95 
96 static int
97 qms_match(device_t parent, cfdata_t cf, void *aux)
98 {
99 	struct qms_attach_args *qa = aux;
100 
101 	if (strcmp(qa->qa_name, "qms") == 0)
102 		return 1;
103 
104 	return 0;
105 }
106 
107 
108 static void
109 qms_attach(device_t parent, device_t self, void *aux)
110 {
111 	struct qms_softc *sc = device_private(self);
112 	struct qms_attach_args *qa = aux;
113 	struct wsmousedev_attach_args wsmouseargs;
114 
115 	sc->sc_dev = self;
116 	sc->sc_iot = qa->qa_iot;
117 	sc->sc_ioh = qa->qa_ioh;
118 	sc->sc_butioh = qa->qa_ioh_but;
119 
120 	/* set up wsmouse attach arguments */
121 	wsmouseargs.accessops = &qms_accessops;
122 	wsmouseargs.accesscookie = sc;
123 
124 	aprint_normal("\n");
125 
126 	sc->sc_wsmousedev =
127 	    config_found(sc->sc_dev, &wsmouseargs, wsmousedevprint, CFARGS_NONE);
128 
129 	callout_init(&sc->sc_callout, 0);
130 }
131 
132 
133 static int
134 qms_enable(void *cookie)
135 {
136 	struct qms_softc *sc = cookie;
137 	int b;
138 
139 	/* We don't want the mouse to warp on open. */
140 	sc->lastx = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
141 	sc->lasty = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
142 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
143 
144 	/* patch up the buttons */
145 	b >>= 4;
146 	sc->lastb = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
147 
148 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
149 	return 0;
150 }
151 
152 
153 static void
154 qms_disable(void *cookie)
155 {
156 	struct qms_softc *sc = cookie;
157 
158 	callout_stop(&sc->sc_callout);
159 }
160 
161 
162 static int
163 qms_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
164 {
165 
166 	switch (cmd) {
167 	case WSMOUSEIO_GTYPE:
168 		*(int *)data = WSMOUSE_TYPE_ARCHIMEDES;
169 		return 0;
170 	}
171 
172 	return EPASSTHROUGH;
173 }
174 
175 
176 static void
177 qms_intr(void *arg)
178 {
179 	struct qms_softc *sc = arg;
180 	int b;
181 	uint16_t x, y;
182 	int16_t dx, dy;
183 
184 	x = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
185 	y = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
186 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
187 
188 	/* patch up the buttons */
189 	b >>= 4;
190 	b = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
191 
192 	if ((x != sc->lastx) || (y != sc->lasty) || (b != sc->lastb)) {
193 		/* This assumes that int16_t is two's complement. */
194 		dx = x - sc->lastx;
195 		dy = y - sc->lasty;
196 		wsmouse_input(sc->sc_wsmousedev,
197 				b,
198 				dx, dy, 0, 0,
199 				WSMOUSE_INPUT_DELTA);
200 
201 		/* save old values */
202 		sc->lastx = x;
203 		sc->lasty = y;
204 		sc->lastb = b;
205 	}
206 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
207 }
208 
209 
210 /* end of qms.c */
211