xref: /netbsd-src/sys/arch/hpcmips/vr/vrdsiu_mouse.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*
2  * Copyright (c) 2001, 2002 Greg Hughes (greg@NetBSD.org). All rights reserved.
3  * Copyright (c) 1999 PocketBSD Project. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Wscons mouse driver for DSIU TrackPoint on IBM WorkPad z50 by
29  * Greg Hughes (greg@NetBSD.org).
30  *
31  * Template for interrupt/device registration taken from vrdsu.c.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: vrdsiu_mouse.c,v 1.9 2007/03/04 05:59:54 christos Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 
41 #include <dev/wscons/wsconsio.h>
42 #include <dev/wscons/wsmousevar.h>
43 
44 #include <machine/bus.h>
45 #include <machine/platid.h>
46 #include <machine/platid_mask.h>
47 
48 #include <hpcmips/vr/vripvar.h>
49 #include <hpcmips/vr/vrdsiureg.h>
50 #include <hpcmips/vr/cmureg.h>
51 
52 enum vrdsiu_mouse_stat {
53 	VRDSIU_MOUSE_STAT_DISABLE,
54 	VRDSIU_MOUSE_STAT_ENABLE
55 };
56 
57 enum vrdsiu_ps2_input_state {
58 	VRDSIU_PS2_INPUT_STATE_BYTE0,
59 	VRDSIU_PS2_INPUT_STATE_BYTE1,
60 	VRDSIU_PS2_INPUT_STATE_BYTE2
61 };
62 
63 struct vrdsiu_softc {
64 	struct device sc_dev;
65 	bus_space_tag_t sc_iot;
66 	bus_space_handle_t sc_ioh;
67 	int sc_unit;
68 	void *sc_handler;
69 	vrip_chipset_tag_t sc_vrip;
70 
71 	enum vrdsiu_mouse_stat sc_mouse_stat;
72 
73 	struct device *sc_wsmousedev;
74 };
75 
76 static int asimOld = 0;
77 
78 static int vrdsiu_match __P((struct device *, struct cfdata *, void *));
79 static void vrdsiu_attach __P((struct device *, struct device *, void *));
80 
81 static void vrdsiu_write __P((struct vrdsiu_softc *, int, unsigned short));
82 static unsigned short vrdsiu_read __P((struct vrdsiu_softc *, int));
83 
84 /* Interrupt handlers */
85 static int vrdsiu_intr __P((void *));
86 static void vrdsiu_mouse_intr __P((struct vrdsiu_softc *));
87 
88 /* Enable/disable DSIU handling */
89 static int vrdsiu_mouse_enable __P((void *));
90 static int vrdsiu_mouse_ioctl __P((void *, u_long, void *, int, struct lwp *));
91 static void vrdsiu_mouse_disable __P((void *));
92 
93 /* wsmouse access ops */
94 const struct wsmouse_accessops vrdsiu_accessops = {
95 	vrdsiu_mouse_enable,
96 	vrdsiu_mouse_ioctl,
97 	vrdsiu_mouse_disable
98 };
99 
100 CFATTACH_DECL(vrdsiu_mouse, sizeof(struct vrdsiu_softc),
101     vrdsiu_match, vrdsiu_attach, NULL, NULL);
102 
103 static inline void
104 vrdsiu_write(sc, port, val)
105 	struct vrdsiu_softc *sc;
106 	int port;
107 	unsigned short val;
108 {
109 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
110 }
111 
112 static inline unsigned short
113 vrdsiu_read(sc, port)
114 	struct vrdsiu_softc *sc;
115 	int port;
116 {
117 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
118 }
119 
120 static int
121 vrdsiu_match(parent, cf, aux)
122 	struct device *parent;
123 	struct cfdata *cf;
124 	void *aux;
125 {
126 	return 1;
127 }
128 
129 static void
130 vrdsiu_attach(parent, self, aux)
131 	struct device *parent;
132 	struct device *self;
133 	void *aux;
134 {
135 	struct vrdsiu_softc *sc = (struct vrdsiu_softc *)self;
136 	struct vrip_attach_args *va = aux;
137 	struct wsmousedev_attach_args wsmaa;
138         int res;
139 
140 	bus_space_tag_t iot = va->va_iot;
141 
142         if (va->va_parent_ioh != 0)
143                 res = bus_space_subregion(iot, va->va_parent_ioh, va->va_addr,
144                     va->va_size, &sc->sc_ioh);
145         else
146                 res = bus_space_map(iot, va->va_addr, va->va_size, 0,
147                     &sc->sc_ioh);
148 	if (res != 0) {
149 		printf(": can't map bus space\n");
150 		return;
151 	}
152 
153 	sc->sc_iot = iot;
154 	sc->sc_unit = va->va_unit;
155 	sc->sc_vrip = va->va_vc;
156 
157 	/* install interrupt handler and enable interrupt */
158 	if (!(sc->sc_handler =
159             vrip_intr_establish(sc->sc_vrip, sc->sc_unit, 0, IPL_TTY,
160                 vrdsiu_intr, sc))) {
161 		printf(": can't map interrupt line\n");
162 		return;
163 	}
164 
165 	/* disable the handler initially */
166 	vrdsiu_mouse_disable(sc);
167 
168 	printf("\n");
169 
170 	/* attach the mouse */
171 	wsmaa.accessops = &vrdsiu_accessops;
172 	wsmaa.accesscookie = sc;
173 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
174 
175 	/*
176 	 * TODO: Initialize the DSIU ourselves.
177 	 *       We currently assume WinCE has set up the DSIU properly
178 	 */
179 
180 	asimOld = vrdsiu_read(sc, DSIUASIM00_REG_W);
181 
182 	/* supply clock to the DSIU */
183 	vrip_power(sc->sc_vrip, sc->sc_unit, 1);
184 }
185 
186 int
187 vrdsiu_mouse_enable(v)
188 	void *v;
189 {
190 	struct vrdsiu_softc *sc = v;
191 
192 	/* ensure the DSIU mouse is currently disabled! */
193 	if (sc->sc_mouse_stat != VRDSIU_MOUSE_STAT_DISABLE)
194 		return EBUSY;
195 
196 	/* enable the DSIU mouse */
197 	sc->sc_mouse_stat = VRDSIU_MOUSE_STAT_ENABLE;
198 
199 	/* unmask interrupts */
200 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, (1 << 8) | (1 << 9) | (1 << 10), 1);
201 	vrip_power(sc->sc_vrip, sc->sc_unit, 1);
202 
203 	return 0;
204 }
205 
206 void
207 vrdsiu_mouse_disable(v)
208 	void *v;
209 {
210 	struct vrdsiu_softc *sc = v;
211 
212 	/* mask interrupts */
213 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, (1 << 8) | (1 << 9) | (1 << 10), 0);
214 
215 	/* disable the DSIU mouse */
216 	sc->sc_mouse_stat = VRDSIU_MOUSE_STAT_DISABLE;
217 }
218 
219 int
220 vrdsiu_mouse_ioctl(v, cmd, data, flag, l)
221 	void *v;
222 	u_long cmd;
223 	void *data;
224 	int flag;
225 	struct lwp *l;
226 {
227 	/*struct vrdsiu_softc *sc = v;*/
228 
229 	switch (cmd)
230 	{
231 	case WSMOUSEIO_GTYPE:
232 		*(u_int *)data = WSMOUSE_TYPE_PS2;
233 		break;
234 
235 	case WSMOUSEIO_SRES:
236 		/*
237 		 * TODO: Handle setting mouse resolution
238 		 */
239 		break;
240 
241 	default:
242 		return -1;
243 	}
244 
245 	return 0;
246 }
247 
248 int
249 vrdsiu_intr(arg)
250 	void *arg;
251 {
252 	struct vrdsiu_softc *sc = arg;
253 
254 	vrdsiu_mouse_intr(sc);
255 
256 	return 0;
257 }
258 
259 /*
260  * PS/2 protocol defines
261  */
262 #define PS2_L_BUTTON_MASK (1 << 0)
263 #define PS2_R_BUTTON_MASK (1 << 1)
264 #define PS2_M_BUTTON_MASK (1 << 2)
265 #define PS2_BYTE0_BIT3_MASK (1 << 3)
266 #define PS2_DX_SIGN_MASK (1 << 4)
267 #define PS2_DY_SIGN_MASK (1 << 5)
268 #define PS2_DX_OVERFLOW_MASK (1 << 6)
269 #define PS2_DY_OVERFLOW_MASK (1 << 7)
270 
271 /*
272  * WSCONS defines
273  */
274 #define WSC_L_BUTTON 0x01
275 #define WSC_M_BUTTON 0x02
276 #define WSC_R_BUTTON 0x04
277 
278 void
279 vrdsiu_mouse_intr(sc)
280 	struct vrdsiu_softc *sc;
281 {
282 	u_int intrReason;
283 	unsigned char b;
284 
285 	static int dx;
286 	static int dy;
287 	static u_char buttons;
288 	static enum vrdsiu_ps2_input_state ps2_state = 0;
289 
290 	/* What caused the interrupt? */
291 	intrReason = vrdsiu_read(sc, DSIUINTR0_REG_W);
292 
293 	/*
294 	 * TODO: Check for error conditions; specifically need to handle
295 	 *       overruns (which currently mess up the mouse).
296 	 */
297 
298 	/* disable reception */
299 	vrdsiu_write(sc, DSIUASIM00_REG_W, asimOld & ~DSIUASIM00_RXE0);
300 
301 	if (intrReason & DSIUINTR0_INTSER0)
302 		ps2_state = 0;
303 
304 	/* Ignore everything except receive notifications */
305 	if ((intrReason & DSIUINTR0_INTSR0) == 0)
306 		goto done;
307 
308 	/* read from DSIU */
309 	b = (unsigned char)vrdsiu_read(sc, DSIURXB0L_REG_W);
310 
311 	/* check if the DSIU is enabled */
312 	if (sc->sc_mouse_stat == VRDSIU_MOUSE_STAT_DISABLE)
313 	{
314 		/* Throw away input to keep the DSIU's buffer clean */
315 		goto done;
316 	}
317 
318 	/*
319 	 * PS/2 protocol interpretation
320 	 *
321 	 * A PS/2 packet consists of 3 bytes.  We read them in, in order, and
322 	 * piece together the mouse info.
323 	 *
324 	 * Byte 0 contains button info and dx/dy signedness
325 	 * Byte 1 contains dx info
326 	 * Byte 2 contains dy info
327 	 *
328 	 * Please see PS/2 specs for detailed information; brief descriptions
329 	 * are provided below.
330 	 *
331 	 * PS/2 mouse specs for the TrackPoint from IBM's TrackPoint Engineering
332 	 * Specification Version 4.0.
333 	 */
334 	switch (ps2_state)
335 	{
336 	case VRDSIU_PS2_INPUT_STATE_BYTE0:
337 		/* Bit 3 of byte 0 is always 1; we use that info to sync input */
338 		if ((b & PS2_BYTE0_BIT3_MASK) == 0)
339 			goto done;
340 
341 		if (b & (PS2_M_BUTTON_MASK | PS2_DX_OVERFLOW_MASK |
342 			PS2_DY_OVERFLOW_MASK))
343 			goto done;
344 
345 		/* Extract button state */
346 		buttons = ((b & PS2_L_BUTTON_MASK) ? WSC_L_BUTTON : 0)
347 			| ((b & PS2_M_BUTTON_MASK) ? WSC_M_BUTTON : 0)
348 			| ((b & PS2_R_BUTTON_MASK) ? WSC_R_BUTTON : 0);
349 
350 		/* Extract dx/dy signedness -- 9-bit 2's comp */
351 		/* dx = (b & PS2_DX_SIGN_MASK) ? 0xFFFFFFFF : 0;
352 		dy = (b & PS2_DY_SIGN_MASK) ? 0xFFFFFFFF : 0; */
353 
354 		ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE1;
355 		break;
356 
357 	case VRDSIU_PS2_INPUT_STATE_BYTE1:
358 		/* put in the lower 8 bits of dx */
359 		dx = (signed char)b;
360 		if (dx == -128)
361 			dx = -127;
362 
363 		ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE2;
364 		break;
365 
366 	case VRDSIU_PS2_INPUT_STATE_BYTE2:
367 		/* put in the lower 8 bits of dy */
368 		dy = (signed char)b;
369 		if (dy == -128)
370 			dy = -127;
371 
372 		/* We now have a complete packet; send to wscons */
373 		wsmouse_input(sc->sc_wsmousedev,
374 				buttons,
375 				dx, dy, 0, 0,
376 				WSMOUSE_INPUT_DELTA);
377 
378 		ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE0;
379 		break;
380 	}
381 
382 done:
383 	/* clear the interrupt */
384 	vrdsiu_write(sc, DSIUINTR0_REG_W, intrReason);
385 
386 	/* enable reception */
387 	vrdsiu_write(sc, DSIUASIM00_REG_W, asimOld | DSIUASIM00_RXE0);
388 }
389