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