ms_pckbport.c revision 1.9 1 1.9 chs /* $NetBSD: ms_pckbport.c,v 1.9 2012/10/27 17:18:11 chs Exp $ */
2 1.1 bjh21
3 1.1 bjh21 /*
4 1.1 bjh21 * Copyright (c) 2002 Valeriy E. Ushakov
5 1.1 bjh21 * All rights reserved.
6 1.1 bjh21 *
7 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
8 1.1 bjh21 * modification, are permitted provided that the following conditions
9 1.1 bjh21 * are met:
10 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
11 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
12 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
14 1.1 bjh21 * documentation and/or other materials provided with the distribution.
15 1.1 bjh21 * 3. The name of the author may not be used to endorse or promote products
16 1.1 bjh21 * derived from this software without specific prior written permission
17 1.1 bjh21 *
18 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 bjh21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 bjh21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 bjh21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 bjh21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 bjh21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 bjh21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 bjh21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 bjh21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 bjh21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 bjh21 */
29 1.1 bjh21 #include <sys/cdefs.h>
30 1.9 chs __KERNEL_RCSID(0, "$NetBSD: ms_pckbport.c,v 1.9 2012/10/27 17:18:11 chs Exp $");
31 1.1 bjh21
32 1.1 bjh21 /*
33 1.1 bjh21 * Attach PS/2 mouse at pckbport aux port
34 1.1 bjh21 * and convert PS/2 mouse protocol to Sun firm events.
35 1.1 bjh21 */
36 1.1 bjh21
37 1.1 bjh21 #include <sys/param.h>
38 1.1 bjh21 #include <sys/systm.h>
39 1.1 bjh21 #include <sys/conf.h>
40 1.1 bjh21 #include <sys/device.h>
41 1.1 bjh21 #include <sys/kernel.h>
42 1.1 bjh21 #include <sys/select.h>
43 1.1 bjh21 #include <sys/proc.h>
44 1.1 bjh21
45 1.1 bjh21 #include <machine/autoconf.h>
46 1.6 dyoung #include <sys/bus.h>
47 1.1 bjh21 #include <machine/intr.h>
48 1.1 bjh21
49 1.1 bjh21 #include <dev/pckbport/pckbportvar.h>
50 1.1 bjh21 #include <dev/pckbport/pmsreg.h>
51 1.1 bjh21
52 1.1 bjh21 #include <machine/vuid_event.h>
53 1.1 bjh21 #include <dev/sun/event_var.h>
54 1.1 bjh21 #include <dev/sun/msvar.h>
55 1.1 bjh21
56 1.1 bjh21 /*
57 1.1 bjh21 * NB: we {re,ab}use ms_softc input translator state and ignore its
58 1.1 bjh21 * zs-related members. Not quite clean, but what the heck.
59 1.1 bjh21 */
60 1.1 bjh21 struct ms_pckbport_softc {
61 1.1 bjh21 struct ms_softc sc_ms;
62 1.1 bjh21
63 1.1 bjh21 /* pckbport attachment */
64 1.1 bjh21 pckbport_tag_t sc_kbctag;
65 1.1 bjh21 pckbport_slot_t sc_kbcslot;
66 1.1 bjh21
67 1.1 bjh21 int sc_enabled; /* input enabled? */
68 1.1 bjh21 };
69 1.1 bjh21
70 1.4 tsutsui static int ms_pckbport_match(device_t, cfdata_t, void *);
71 1.4 tsutsui static void ms_pckbport_attach(device_t, device_t, void *);
72 1.1 bjh21
73 1.4 tsutsui CFATTACH_DECL_NEW(ms_pckbport, sizeof(struct ms_pckbport_softc),
74 1.1 bjh21 ms_pckbport_match, ms_pckbport_attach, NULL, NULL);
75 1.1 bjh21
76 1.1 bjh21
77 1.9 chs static int ms_pckbport_iopen(device_t, int);
78 1.9 chs static int ms_pckbport_iclose(device_t, int);
79 1.1 bjh21 static void ms_pckbport_input(void *, int);
80 1.1 bjh21
81 1.1 bjh21
82 1.1 bjh21 static int
83 1.4 tsutsui ms_pckbport_match(device_t parent, cfdata_t cf, void *aux)
84 1.1 bjh21 {
85 1.1 bjh21 struct pckbport_attach_args *pa = aux;
86 1.2 uwe
87 1.1 bjh21 return (pa->pa_slot == PCKBPORT_AUX_SLOT);
88 1.1 bjh21 }
89 1.1 bjh21
90 1.1 bjh21
91 1.1 bjh21 static void
92 1.4 tsutsui ms_pckbport_attach(device_t parent, device_t self, void *aux)
93 1.1 bjh21 {
94 1.4 tsutsui struct ms_pckbport_softc *sc = device_private(self);
95 1.1 bjh21 struct ms_softc *ms = &sc->sc_ms;
96 1.1 bjh21 struct pckbport_attach_args *pa = aux;
97 1.1 bjh21
98 1.1 bjh21 u_char cmd[1], resp[2];
99 1.1 bjh21 int res;
100 1.1 bjh21
101 1.4 tsutsui ms->ms_dev = self;
102 1.4 tsutsui
103 1.1 bjh21 /* save our pckbport attachment */
104 1.1 bjh21 sc->sc_kbctag = pa->pa_tag;
105 1.1 bjh21 sc->sc_kbcslot = pa->pa_slot;
106 1.1 bjh21
107 1.1 bjh21 /* Hooks called by upper layer on device open/close */
108 1.1 bjh21 ms->ms_deviopen = ms_pckbport_iopen;
109 1.1 bjh21 ms->ms_deviclose = ms_pckbport_iclose;
110 1.1 bjh21
111 1.4 tsutsui aprint_normal("\n");
112 1.1 bjh21
113 1.1 bjh21 /* reset the device */
114 1.1 bjh21 cmd[0] = PMS_RESET;
115 1.1 bjh21 res = pckbport_poll_cmd(sc->sc_kbctag, sc->sc_kbcslot,
116 1.1 bjh21 cmd, 1, 2, resp, 1);
117 1.1 bjh21 #ifdef DIAGNOSTIC
118 1.1 bjh21 if (res || resp[0] != PMS_RSTDONE || resp[1] != 0) {
119 1.4 tsutsui aprint_error("%s: reset error\n", __func__);
120 1.1 bjh21 /* return; */
121 1.1 bjh21 }
122 1.1 bjh21 #endif
123 1.1 bjh21
124 1.1 bjh21 pckbport_set_inputhandler(sc->sc_kbctag, sc->sc_kbcslot,
125 1.4 tsutsui ms_pckbport_input, sc, device_xname(self));
126 1.1 bjh21
127 1.1 bjh21 /* no interrupts until device is actually opened */
128 1.1 bjh21 cmd[0] = PMS_DEV_DISABLE;
129 1.1 bjh21 res = pckbport_poll_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
130 1.1 bjh21 1, 0, 0, 0);
131 1.1 bjh21 if (res)
132 1.4 tsutsui aprint_error("%s: failed to disable interrupts\n", __func__);
133 1.1 bjh21 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
134 1.1 bjh21 }
135 1.1 bjh21
136 1.1 bjh21
137 1.1 bjh21 static int
138 1.9 chs ms_pckbport_iopen(device_t self, int flags)
139 1.1 bjh21 {
140 1.4 tsutsui struct ms_pckbport_softc *sc = device_private(self);
141 1.1 bjh21 struct ms_softc *ms = &sc->sc_ms;
142 1.1 bjh21 u_char cmd[1];
143 1.1 bjh21 int res;
144 1.1 bjh21
145 1.1 bjh21 ms->ms_byteno = 0;
146 1.1 bjh21 ms->ms_dx = ms->ms_dy = 0;
147 1.1 bjh21 ms->ms_ub = ms->ms_mb = 0;
148 1.1 bjh21
149 1.1 bjh21 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 1);
150 1.1 bjh21
151 1.1 bjh21 cmd[0] = PMS_DEV_ENABLE;
152 1.1 bjh21 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot,
153 1.1 bjh21 cmd, 1, 0, 1, NULL);
154 1.1 bjh21 if (res) {
155 1.7 jakllsch printf("%s: command error\n", __func__);
156 1.1 bjh21 return (res);
157 1.1 bjh21 }
158 1.1 bjh21
159 1.1 bjh21 sc->sc_enabled = 1;
160 1.1 bjh21 return (0);
161 1.1 bjh21 }
162 1.1 bjh21
163 1.1 bjh21
164 1.1 bjh21 static int
165 1.9 chs ms_pckbport_iclose(device_t self, int flags)
166 1.1 bjh21 {
167 1.4 tsutsui struct ms_pckbport_softc *sc = device_private(self);
168 1.1 bjh21 u_char cmd[1];
169 1.1 bjh21 int res;
170 1.1 bjh21
171 1.1 bjh21 cmd[0] = PMS_DEV_DISABLE;
172 1.1 bjh21 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot,
173 1.1 bjh21 cmd, 1, 0, 1, NULL);
174 1.1 bjh21 if (res)
175 1.8 jakllsch printf("%s: command error\n", __func__);
176 1.1 bjh21
177 1.1 bjh21 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
178 1.1 bjh21
179 1.1 bjh21 sc->sc_enabled = 0;
180 1.1 bjh21 return (0);
181 1.1 bjh21 }
182 1.1 bjh21
183 1.1 bjh21
184 1.1 bjh21 /* Masks for the first byte of a PS/2 mouse packet */
185 1.1 bjh21 #define PS2LBUTMASK 0x01
186 1.1 bjh21 #define PS2RBUTMASK 0x02
187 1.1 bjh21 #define PS2MBUTMASK 0x04
188 1.1 bjh21
189 1.1 bjh21 /*
190 1.1 bjh21 * Got a receive interrupt - pckbport wants to give us a byte.
191 1.1 bjh21 */
192 1.1 bjh21 static void
193 1.2 uwe ms_pckbport_input(void *vsc, int data)
194 1.1 bjh21 {
195 1.1 bjh21 struct ms_pckbport_softc *sc = vsc;
196 1.1 bjh21 struct ms_softc *ms = &sc->sc_ms;
197 1.1 bjh21 struct firm_event *fe;
198 1.1 bjh21 int mb, ub, d, get, put, any;
199 1.1 bjh21
200 1.1 bjh21 /* map changed buttons mask to the highest bit */
201 1.1 bjh21 static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
202 1.1 bjh21
203 1.1 bjh21 /* map bits to mouse buttons */
204 1.1 bjh21 static const int to_id[] = { MS_LEFT, MS_MIDDLE, 0, MS_RIGHT };
205 1.1 bjh21
206 1.1 bjh21 if (!sc->sc_enabled) {
207 1.1 bjh21 /* Interrupts are not expected. Discard the byte. */
208 1.1 bjh21 return;
209 1.1 bjh21 }
210 1.1 bjh21
211 1.1 bjh21 switch (ms->ms_byteno) {
212 1.1 bjh21
213 1.1 bjh21 case 0:
214 1.1 bjh21 if ((data & 0xc0) == 0) { /* no ovfl, bit 3 == 1 too? */
215 1.2 uwe ms->ms_mb =
216 1.1 bjh21 ((data & PS2LBUTMASK) ? 0x1 : 0) |
217 1.1 bjh21 ((data & PS2MBUTMASK) ? 0x2 : 0) |
218 1.1 bjh21 ((data & PS2RBUTMASK) ? 0x4 : 0) ;
219 1.1 bjh21 ++ms->ms_byteno;
220 1.1 bjh21 }
221 1.1 bjh21 return;
222 1.1 bjh21
223 1.1 bjh21 case 1:
224 1.1 bjh21 ms->ms_dx += (int8_t)data;
225 1.1 bjh21 ++ms->ms_byteno;
226 1.1 bjh21 return;
227 1.1 bjh21
228 1.1 bjh21 case 2:
229 1.1 bjh21 ms->ms_dy += (int8_t)data;
230 1.1 bjh21 ms->ms_byteno = 0;
231 1.1 bjh21 break; /* last byte processed, report changes */
232 1.1 bjh21 }
233 1.1 bjh21
234 1.1 bjh21 any = 0;
235 1.1 bjh21 get = ms->ms_events.ev_get;
236 1.1 bjh21 put = ms->ms_events.ev_put;
237 1.1 bjh21 fe = &ms->ms_events.ev_q[put];
238 1.1 bjh21
239 1.1 bjh21 /* NEXT prepares to put the next event, backing off if necessary */
240 1.1 bjh21 #define NEXT do { \
241 1.1 bjh21 if ((++put) % EV_QSIZE == get) { \
242 1.1 bjh21 --put; \
243 1.1 bjh21 goto out; \
244 1.1 bjh21 } \
245 1.1 bjh21 } while (0)
246 1.1 bjh21
247 1.1 bjh21 /* ADVANCE completes the `put' of the event */
248 1.1 bjh21 #define ADVANCE do { \
249 1.1 bjh21 ++fe; \
250 1.1 bjh21 if (put >= EV_QSIZE) { \
251 1.1 bjh21 put = 0; \
252 1.1 bjh21 fe = &ms->ms_events.ev_q[0]; \
253 1.1 bjh21 } \
254 1.1 bjh21 any = 1; \
255 1.1 bjh21 } while (0)
256 1.1 bjh21
257 1.1 bjh21 ub = ms->ms_ub; /* old buttons state */
258 1.1 bjh21 mb = ms->ms_mb; /* new buttons state */
259 1.1 bjh21 while ((d = mb ^ ub) != 0) {
260 1.1 bjh21 /*
261 1.1 bjh21 * Mouse button change. Convert up to three state changes
262 1.1 bjh21 * to the `first' change, and drop it into the event queue.
263 1.1 bjh21 */
264 1.1 bjh21 NEXT;
265 1.1 bjh21 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
266 1.1 bjh21 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
267 1.1 bjh21 fe->value = (mb & d) ? VKEY_DOWN : VKEY_UP;
268 1.5 he firm_gettime(fe);
269 1.1 bjh21 ADVANCE;
270 1.1 bjh21 ub ^= d; /* reflect the button state change */
271 1.1 bjh21 }
272 1.1 bjh21
273 1.1 bjh21 if (ms->ms_dx != 0) {
274 1.1 bjh21 NEXT;
275 1.1 bjh21 fe->id = LOC_X_DELTA;
276 1.1 bjh21 fe->value = ms->ms_dx;
277 1.5 he firm_gettime(fe);
278 1.1 bjh21 ADVANCE;
279 1.1 bjh21 ms->ms_dx = 0;
280 1.1 bjh21 }
281 1.1 bjh21
282 1.1 bjh21 if (ms->ms_dy != 0) {
283 1.1 bjh21 NEXT;
284 1.1 bjh21 fe->id = LOC_Y_DELTA;
285 1.1 bjh21 fe->value = ms->ms_dy;
286 1.5 he firm_gettime(fe);
287 1.1 bjh21 ADVANCE;
288 1.1 bjh21 ms->ms_dy = 0;
289 1.1 bjh21 }
290 1.1 bjh21
291 1.1 bjh21 out:
292 1.1 bjh21 if (any) {
293 1.1 bjh21 ms->ms_ub = ub; /* save button state */
294 1.1 bjh21 ms->ms_events.ev_put = put;
295 1.1 bjh21 EV_WAKEUP(&ms->ms_events);
296 1.1 bjh21 }
297 1.1 bjh21 }
298