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.10 2009/03/14 14:46:00 dsl 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
52enum vrdsiu_mouse_stat {
53	VRDSIU_MOUSE_STAT_DISABLE,
54	VRDSIU_MOUSE_STAT_ENABLE
55};
56
57enum vrdsiu_ps2_input_state {
58	VRDSIU_PS2_INPUT_STATE_BYTE0,
59	VRDSIU_PS2_INPUT_STATE_BYTE1,
60	VRDSIU_PS2_INPUT_STATE_BYTE2
61};
62
63struct 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
76static int asimOld = 0;
77
78static int vrdsiu_match(struct device *, struct cfdata *, void *);
79static void vrdsiu_attach(struct device *, struct device *, void *);
80
81static void vrdsiu_write(struct vrdsiu_softc *, int, unsigned short);
82static unsigned short vrdsiu_read(struct vrdsiu_softc *, int);
83
84/* Interrupt handlers */
85static int vrdsiu_intr(void *);
86static void vrdsiu_mouse_intr(struct vrdsiu_softc *);
87
88/* Enable/disable DSIU handling */
89static int vrdsiu_mouse_enable(void *);
90static int vrdsiu_mouse_ioctl(void *, u_long, void *, int, struct lwp *);
91static void vrdsiu_mouse_disable(void *);
92
93/* wsmouse access ops */
94const struct wsmouse_accessops vrdsiu_accessops = {
95	vrdsiu_mouse_enable,
96	vrdsiu_mouse_ioctl,
97	vrdsiu_mouse_disable
98};
99
100CFATTACH_DECL(vrdsiu_mouse, sizeof(struct vrdsiu_softc),
101    vrdsiu_match, vrdsiu_attach, NULL, NULL);
102
103static inline void
104vrdsiu_write(struct vrdsiu_softc *sc, int port, unsigned short val)
105{
106	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
107}
108
109static inline unsigned short
110vrdsiu_read(struct vrdsiu_softc *sc, int port)
111{
112	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
113}
114
115static int
116vrdsiu_match(struct device *parent, struct cfdata *cf, void *aux)
117{
118	return 1;
119}
120
121static void
122vrdsiu_attach(struct device *parent, struct device *self, void *aux)
123{
124	struct vrdsiu_softc *sc = (struct vrdsiu_softc *)self;
125	struct vrip_attach_args *va = aux;
126	struct wsmousedev_attach_args wsmaa;
127        int res;
128
129	bus_space_tag_t iot = va->va_iot;
130
131        if (va->va_parent_ioh != 0)
132                res = bus_space_subregion(iot, va->va_parent_ioh, va->va_addr,
133                    va->va_size, &sc->sc_ioh);
134        else
135                res = bus_space_map(iot, va->va_addr, va->va_size, 0,
136                    &sc->sc_ioh);
137	if (res != 0) {
138		printf(": can't map bus space\n");
139		return;
140	}
141
142	sc->sc_iot = iot;
143	sc->sc_unit = va->va_unit;
144	sc->sc_vrip = va->va_vc;
145
146	/* install interrupt handler and enable interrupt */
147	if (!(sc->sc_handler =
148            vrip_intr_establish(sc->sc_vrip, sc->sc_unit, 0, IPL_TTY,
149                vrdsiu_intr, sc))) {
150		printf(": can't map interrupt line\n");
151		return;
152	}
153
154	/* disable the handler initially */
155	vrdsiu_mouse_disable(sc);
156
157	printf("\n");
158
159	/* attach the mouse */
160	wsmaa.accessops = &vrdsiu_accessops;
161	wsmaa.accesscookie = sc;
162	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
163
164	/*
165	 * TODO: Initialize the DSIU ourselves.
166	 *       We currently assume WinCE has set up the DSIU properly
167	 */
168
169	asimOld = vrdsiu_read(sc, DSIUASIM00_REG_W);
170
171	/* supply clock to the DSIU */
172	vrip_power(sc->sc_vrip, sc->sc_unit, 1);
173}
174
175int
176vrdsiu_mouse_enable(void *v)
177{
178	struct vrdsiu_softc *sc = v;
179
180	/* ensure the DSIU mouse is currently disabled! */
181	if (sc->sc_mouse_stat != VRDSIU_MOUSE_STAT_DISABLE)
182		return EBUSY;
183
184	/* enable the DSIU mouse */
185	sc->sc_mouse_stat = VRDSIU_MOUSE_STAT_ENABLE;
186
187	/* unmask interrupts */
188	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, (1 << 8) | (1 << 9) | (1 << 10), 1);
189	vrip_power(sc->sc_vrip, sc->sc_unit, 1);
190
191	return 0;
192}
193
194void
195vrdsiu_mouse_disable(void *v)
196{
197	struct vrdsiu_softc *sc = v;
198
199	/* mask interrupts */
200	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, (1 << 8) | (1 << 9) | (1 << 10), 0);
201
202	/* disable the DSIU mouse */
203	sc->sc_mouse_stat = VRDSIU_MOUSE_STAT_DISABLE;
204}
205
206int
207vrdsiu_mouse_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
208{
209	/*struct vrdsiu_softc *sc = v;*/
210
211	switch (cmd)
212	{
213	case WSMOUSEIO_GTYPE:
214		*(u_int *)data = WSMOUSE_TYPE_PS2;
215		break;
216
217	case WSMOUSEIO_SRES:
218		/*
219		 * TODO: Handle setting mouse resolution
220		 */
221		break;
222
223	default:
224		return -1;
225	}
226
227	return 0;
228}
229
230int
231vrdsiu_intr(void *arg)
232{
233	struct vrdsiu_softc *sc = arg;
234
235	vrdsiu_mouse_intr(sc);
236
237	return 0;
238}
239
240/*
241 * PS/2 protocol defines
242 */
243#define PS2_L_BUTTON_MASK (1 << 0)
244#define PS2_R_BUTTON_MASK (1 << 1)
245#define PS2_M_BUTTON_MASK (1 << 2)
246#define PS2_BYTE0_BIT3_MASK (1 << 3)
247#define PS2_DX_SIGN_MASK (1 << 4)
248#define PS2_DY_SIGN_MASK (1 << 5)
249#define PS2_DX_OVERFLOW_MASK (1 << 6)
250#define PS2_DY_OVERFLOW_MASK (1 << 7)
251
252/*
253 * WSCONS defines
254 */
255#define WSC_L_BUTTON 0x01
256#define WSC_M_BUTTON 0x02
257#define WSC_R_BUTTON 0x04
258
259void
260vrdsiu_mouse_intr(struct vrdsiu_softc *sc)
261{
262	u_int intrReason;
263	unsigned char b;
264
265	static int dx;
266	static int dy;
267	static u_char buttons;
268	static enum vrdsiu_ps2_input_state ps2_state = 0;
269
270	/* What caused the interrupt? */
271	intrReason = vrdsiu_read(sc, DSIUINTR0_REG_W);
272
273	/*
274	 * TODO: Check for error conditions; specifically need to handle
275	 *       overruns (which currently mess up the mouse).
276	 */
277
278	/* disable reception */
279	vrdsiu_write(sc, DSIUASIM00_REG_W, asimOld & ~DSIUASIM00_RXE0);
280
281	if (intrReason & DSIUINTR0_INTSER0)
282		ps2_state = 0;
283
284	/* Ignore everything except receive notifications */
285	if ((intrReason & DSIUINTR0_INTSR0) == 0)
286		goto done;
287
288	/* read from DSIU */
289	b = (unsigned char)vrdsiu_read(sc, DSIURXB0L_REG_W);
290
291	/* check if the DSIU is enabled */
292	if (sc->sc_mouse_stat == VRDSIU_MOUSE_STAT_DISABLE)
293	{
294		/* Throw away input to keep the DSIU's buffer clean */
295		goto done;
296	}
297
298	/*
299	 * PS/2 protocol interpretation
300	 *
301	 * A PS/2 packet consists of 3 bytes.  We read them in, in order, and
302	 * piece together the mouse info.
303	 *
304	 * Byte 0 contains button info and dx/dy signedness
305	 * Byte 1 contains dx info
306	 * Byte 2 contains dy info
307	 *
308	 * Please see PS/2 specs for detailed information; brief descriptions
309	 * are provided below.
310	 *
311	 * PS/2 mouse specs for the TrackPoint from IBM's TrackPoint Engineering
312	 * Specification Version 4.0.
313	 */
314	switch (ps2_state)
315	{
316	case VRDSIU_PS2_INPUT_STATE_BYTE0:
317		/* Bit 3 of byte 0 is always 1; we use that info to sync input */
318		if ((b & PS2_BYTE0_BIT3_MASK) == 0)
319			goto done;
320
321		if (b & (PS2_M_BUTTON_MASK | PS2_DX_OVERFLOW_MASK |
322			PS2_DY_OVERFLOW_MASK))
323			goto done;
324
325		/* Extract button state */
326		buttons = ((b & PS2_L_BUTTON_MASK) ? WSC_L_BUTTON : 0)
327			| ((b & PS2_M_BUTTON_MASK) ? WSC_M_BUTTON : 0)
328			| ((b & PS2_R_BUTTON_MASK) ? WSC_R_BUTTON : 0);
329
330		/* Extract dx/dy signedness -- 9-bit 2's comp */
331		/* dx = (b & PS2_DX_SIGN_MASK) ? 0xFFFFFFFF : 0;
332		dy = (b & PS2_DY_SIGN_MASK) ? 0xFFFFFFFF : 0; */
333
334		ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE1;
335		break;
336
337	case VRDSIU_PS2_INPUT_STATE_BYTE1:
338		/* put in the lower 8 bits of dx */
339		dx = (signed char)b;
340		if (dx == -128)
341			dx = -127;
342
343		ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE2;
344		break;
345
346	case VRDSIU_PS2_INPUT_STATE_BYTE2:
347		/* put in the lower 8 bits of dy */
348		dy = (signed char)b;
349		if (dy == -128)
350			dy = -127;
351
352		/* We now have a complete packet; send to wscons */
353		wsmouse_input(sc->sc_wsmousedev,
354				buttons,
355				dx, dy, 0, 0,
356				WSMOUSE_INPUT_DELTA);
357
358		ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE0;
359		break;
360	}
361
362done:
363	/* clear the interrupt */
364	vrdsiu_write(sc, DSIUINTR0_REG_W, intrReason);
365
366	/* enable reception */
367	vrdsiu_write(sc, DSIUASIM00_REG_W, asimOld | DSIUASIM00_RXE0);
368}
369