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