xref: /netbsd-src/sys/arch/arm/iomd/qms.c (revision f82ca6eefb335bf699131a4ebe4cc00c8911db8a)
1 /*	$NetBSD: qms.c,v 1.22 2022/09/27 06:36:42 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  *
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.22 2022/09/27 06:36:42 skrll 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/proc.h>
45 #include <sys/tty.h>
46 #include <sys/types.h>
47 #include <sys/syslog.h>
48 #include <sys/systm.h>
49 #include <sys/select.h>
50 
51 #include <sys/bus.h>
52 #include <machine/intr.h>
53 
54 #include <arm/iomd/iomdvar.h>
55 
56 #include <dev/wscons/wsconsio.h>
57 #include <dev/wscons/wsmousevar.h>
58 
59 struct qms_softc {
60 	device_t sc_dev;
61 	device_t sc_wsmousedev;
62 
63 	bus_space_tag_t sc_iot;		/* bus tag */
64 	bus_space_handle_t sc_ioh;	/* bus handle for XY */
65 	bus_space_handle_t sc_butioh;	/* bus handle for buttons */
66 
67 	struct callout sc_callout;
68 
69 	uint16_t lastx;
70 	uint16_t lasty;
71 	int lastb;
72 };
73 
74 /* Offsets of hardware registers */
75 #define QMS_MOUSEX	0		/* 16 bits X register */
76 #define QMS_MOUSEY	1		/* 16 bits Y register */
77 #define QMS_BUTTONS	0 		/* mouse buttons in bits 4,5,6 */
78 
79 static int  qms_match(device_t , cfdata_t , void *);
80 static void qms_attach(device_t , device_t , void *);
81 
82 static int qms_enable(void *);
83 static int qms_ioctl(void *, u_long, void *, int, struct lwp *);
84 static void qms_disable(void *cookie);
85 static void qms_intr(void *arg);
86 
87 CFATTACH_DECL_NEW(qms, sizeof(struct qms_softc),
88     qms_match, qms_attach, NULL, NULL);
89 
90 static struct wsmouse_accessops qms_accessops = {
91 	qms_enable, qms_ioctl, qms_disable
92 };
93 
94 
95 static int
qms_match(device_t parent,cfdata_t cf,void * aux)96 qms_match(device_t parent, cfdata_t cf, void *aux)
97 {
98 	struct qms_attach_args *qa = aux;
99 
100 	if (strcmp(qa->qa_name, "qms") == 0)
101 		return 1;
102 
103 	return 0;
104 }
105 
106 
107 static void
qms_attach(device_t parent,device_t self,void * aux)108 qms_attach(device_t parent, device_t self, void *aux)
109 {
110 	struct qms_softc *sc = device_private(self);
111 	struct qms_attach_args *qa = aux;
112 	struct wsmousedev_attach_args wsmouseargs;
113 
114 	sc->sc_dev = self;
115 	sc->sc_iot = qa->qa_iot;
116 	sc->sc_ioh = qa->qa_ioh;
117 	sc->sc_butioh = qa->qa_ioh_but;
118 
119 	/* set up wsmouse attach arguments */
120 	wsmouseargs.accessops = &qms_accessops;
121 	wsmouseargs.accesscookie = sc;
122 
123 	aprint_normal("\n");
124 
125 	sc->sc_wsmousedev =
126 	    config_found(sc->sc_dev, &wsmouseargs, wsmousedevprint, CFARGS_NONE);
127 
128 	callout_init(&sc->sc_callout, 0);
129 }
130 
131 
132 static int
qms_enable(void * cookie)133 qms_enable(void *cookie)
134 {
135 	struct qms_softc *sc = cookie;
136 	int b;
137 
138 	/* We don't want the mouse to warp on open. */
139 	sc->lastx = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
140 	sc->lasty = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
141 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
142 
143 	/* patch up the buttons */
144 	b >>= 4;
145 	sc->lastb = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
146 
147 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
148 	return 0;
149 }
150 
151 
152 static void
qms_disable(void * cookie)153 qms_disable(void *cookie)
154 {
155 	struct qms_softc *sc = cookie;
156 
157 	callout_stop(&sc->sc_callout);
158 }
159 
160 
161 static int
qms_ioctl(void * cookie,u_long cmd,void * data,int flag,struct lwp * l)162 qms_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
163 {
164 
165 	switch (cmd) {
166 	case WSMOUSEIO_GTYPE:
167 		*(int *)data = WSMOUSE_TYPE_ARCHIMEDES;
168 		return 0;
169 	}
170 
171 	return EPASSTHROUGH;
172 }
173 
174 
175 static void
qms_intr(void * arg)176 qms_intr(void *arg)
177 {
178 	struct qms_softc *sc = arg;
179 	int b;
180 	uint16_t x, y;
181 	int16_t dx, dy;
182 
183 	x = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
184 	y = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
185 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
186 
187 	/* patch up the buttons */
188 	b >>= 4;
189 	b = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
190 
191 	if ((x != sc->lastx) || (y != sc->lasty) || (b != sc->lastb)) {
192 		/* This assumes that int16_t is two's complement. */
193 		dx = x - sc->lastx;
194 		dy = y - sc->lasty;
195 		wsmouse_input(sc->sc_wsmousedev,
196 				b,
197 				dx, dy, 0, 0,
198 				WSMOUSE_INPUT_DELTA);
199 
200 		/* save old values */
201 		sc->lastx = x;
202 		sc->lasty = y;
203 		sc->lastb = b;
204 	}
205 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
206 }
207 
208 
209 /* end of qms.c */
210