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.14 2021/08/07 16:18:54 thorpej 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
vrdsiu_write(struct vrdsiu_softc * sc,int port,unsigned short val)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
vrdsiu_read(struct vrdsiu_softc * sc,int port)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
vrdsiu_match(device_t parent,cfdata_t cf,void * aux)115 vrdsiu_match(device_t parent, cfdata_t cf, void *aux)
116 {
117 return 1;
118 }
119
120 static void
vrdsiu_attach(device_t parent,device_t self,void * aux)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 CFARGS_NONE);
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
175 int
vrdsiu_mouse_enable(void * v)176 vrdsiu_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
194 void
vrdsiu_mouse_disable(void * v)195 vrdsiu_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
206 int
vrdsiu_mouse_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)207 vrdsiu_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
230 int
vrdsiu_intr(void * arg)231 vrdsiu_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
259 void
vrdsiu_mouse_intr(struct vrdsiu_softc * sc)260 vrdsiu_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
362 done:
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