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