ms_pckbport.c revision 1.3 1 1.3 kardel /* $NetBSD: ms_pckbport.c,v 1.3 2006/06/07 22:38:49 kardel 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.3 kardel __KERNEL_RCSID(0, "$NetBSD: ms_pckbport.c,v 1.3 2006/06/07 22:38:49 kardel 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.1 bjh21 #include <machine/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.1 bjh21 static int ms_pckbport_match(struct device *, struct cfdata *, void *);
71 1.1 bjh21 static void ms_pckbport_attach(struct device *, struct device *, void *);
72 1.1 bjh21
73 1.1 bjh21 CFATTACH_DECL(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.1 bjh21 static int ms_pckbport_iopen(struct device *, int);
78 1.1 bjh21 static int ms_pckbport_iclose(struct device *, 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.2 uwe ms_pckbport_match(struct device *parent, struct cfdata *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.2 uwe ms_pckbport_attach(struct device *parent, struct device *self, void *aux)
93 1.1 bjh21 {
94 1.1 bjh21 struct ms_pckbport_softc *sc = (struct ms_pckbport_softc *)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.1 bjh21 /* save our pckbport attachment */
102 1.1 bjh21 sc->sc_kbctag = pa->pa_tag;
103 1.1 bjh21 sc->sc_kbcslot = pa->pa_slot;
104 1.1 bjh21
105 1.1 bjh21 /* Hooks called by upper layer on device open/close */
106 1.1 bjh21 ms->ms_deviopen = ms_pckbport_iopen;
107 1.1 bjh21 ms->ms_deviclose = ms_pckbport_iclose;
108 1.1 bjh21
109 1.1 bjh21 printf("\n");
110 1.1 bjh21
111 1.1 bjh21 /* reset the device */
112 1.1 bjh21 cmd[0] = PMS_RESET;
113 1.1 bjh21 res = pckbport_poll_cmd(sc->sc_kbctag, sc->sc_kbcslot,
114 1.1 bjh21 cmd, 1, 2, resp, 1);
115 1.1 bjh21 #ifdef DIAGNOSTIC
116 1.1 bjh21 if (res || resp[0] != PMS_RSTDONE || resp[1] != 0) {
117 1.1 bjh21 printf("ms_pckbport_attach: reset error\n");
118 1.1 bjh21 /* return; */
119 1.1 bjh21 }
120 1.1 bjh21 #endif
121 1.1 bjh21
122 1.1 bjh21 pckbport_set_inputhandler(sc->sc_kbctag, sc->sc_kbcslot,
123 1.1 bjh21 ms_pckbport_input, sc, ms->ms_dev.dv_xname);
124 1.1 bjh21
125 1.1 bjh21 /* no interrupts until device is actually opened */
126 1.1 bjh21 cmd[0] = PMS_DEV_DISABLE;
127 1.1 bjh21 res = pckbport_poll_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
128 1.1 bjh21 1, 0, 0, 0);
129 1.1 bjh21 if (res)
130 1.1 bjh21 printf("ms_pckbport_attach: failed to disable interrupts\n");
131 1.1 bjh21 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
132 1.1 bjh21 }
133 1.1 bjh21
134 1.1 bjh21
135 1.1 bjh21 static int
136 1.2 uwe ms_pckbport_iopen(struct device *self, int flags)
137 1.1 bjh21 {
138 1.1 bjh21 struct ms_pckbport_softc *sc = (struct ms_pckbport_softc *)self;
139 1.1 bjh21 struct ms_softc *ms = &sc->sc_ms;
140 1.1 bjh21 u_char cmd[1];
141 1.1 bjh21 int res;
142 1.1 bjh21
143 1.1 bjh21 ms->ms_byteno = 0;
144 1.1 bjh21 ms->ms_dx = ms->ms_dy = 0;
145 1.1 bjh21 ms->ms_ub = ms->ms_mb = 0;
146 1.1 bjh21
147 1.1 bjh21 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 1);
148 1.1 bjh21
149 1.1 bjh21 cmd[0] = PMS_DEV_ENABLE;
150 1.1 bjh21 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot,
151 1.1 bjh21 cmd, 1, 0, 1, NULL);
152 1.1 bjh21 if (res) {
153 1.1 bjh21 printf("pms_enable: command error\n");
154 1.1 bjh21 return (res);
155 1.1 bjh21 }
156 1.1 bjh21
157 1.1 bjh21 sc->sc_enabled = 1;
158 1.1 bjh21 return (0);
159 1.1 bjh21 }
160 1.1 bjh21
161 1.1 bjh21
162 1.1 bjh21 static int
163 1.2 uwe ms_pckbport_iclose(struct device *self, int flags)
164 1.1 bjh21 {
165 1.1 bjh21 struct ms_pckbport_softc *sc = (struct ms_pckbport_softc *)self;
166 1.1 bjh21 u_char cmd[1];
167 1.1 bjh21 int res;
168 1.1 bjh21
169 1.1 bjh21 cmd[0] = PMS_DEV_DISABLE;
170 1.1 bjh21 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot,
171 1.1 bjh21 cmd, 1, 0, 1, NULL);
172 1.1 bjh21 if (res)
173 1.1 bjh21 printf("pms_disable: command error\n");
174 1.1 bjh21
175 1.1 bjh21 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
176 1.1 bjh21
177 1.1 bjh21 sc->sc_enabled = 0;
178 1.1 bjh21 return (0);
179 1.1 bjh21 }
180 1.1 bjh21
181 1.1 bjh21
182 1.1 bjh21 /* Masks for the first byte of a PS/2 mouse packet */
183 1.1 bjh21 #define PS2LBUTMASK 0x01
184 1.1 bjh21 #define PS2RBUTMASK 0x02
185 1.1 bjh21 #define PS2MBUTMASK 0x04
186 1.1 bjh21
187 1.1 bjh21 /*
188 1.1 bjh21 * Got a receive interrupt - pckbport wants to give us a byte.
189 1.1 bjh21 */
190 1.1 bjh21 static void
191 1.2 uwe ms_pckbport_input(void *vsc, int data)
192 1.1 bjh21 {
193 1.1 bjh21 struct ms_pckbport_softc *sc = vsc;
194 1.1 bjh21 struct ms_softc *ms = &sc->sc_ms;
195 1.1 bjh21 struct firm_event *fe;
196 1.1 bjh21 int mb, ub, d, get, put, any;
197 1.1 bjh21
198 1.1 bjh21 /* map changed buttons mask to the highest bit */
199 1.1 bjh21 static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
200 1.1 bjh21
201 1.1 bjh21 /* map bits to mouse buttons */
202 1.1 bjh21 static const int to_id[] = { MS_LEFT, MS_MIDDLE, 0, MS_RIGHT };
203 1.1 bjh21
204 1.1 bjh21 if (!sc->sc_enabled) {
205 1.1 bjh21 /* Interrupts are not expected. Discard the byte. */
206 1.1 bjh21 return;
207 1.1 bjh21 }
208 1.1 bjh21
209 1.1 bjh21 switch (ms->ms_byteno) {
210 1.1 bjh21
211 1.1 bjh21 case 0:
212 1.1 bjh21 if ((data & 0xc0) == 0) { /* no ovfl, bit 3 == 1 too? */
213 1.2 uwe ms->ms_mb =
214 1.1 bjh21 ((data & PS2LBUTMASK) ? 0x1 : 0) |
215 1.1 bjh21 ((data & PS2MBUTMASK) ? 0x2 : 0) |
216 1.1 bjh21 ((data & PS2RBUTMASK) ? 0x4 : 0) ;
217 1.1 bjh21 ++ms->ms_byteno;
218 1.1 bjh21 }
219 1.1 bjh21 return;
220 1.1 bjh21
221 1.1 bjh21 case 1:
222 1.1 bjh21 ms->ms_dx += (int8_t)data;
223 1.1 bjh21 ++ms->ms_byteno;
224 1.1 bjh21 return;
225 1.1 bjh21
226 1.1 bjh21 case 2:
227 1.1 bjh21 ms->ms_dy += (int8_t)data;
228 1.1 bjh21 ms->ms_byteno = 0;
229 1.1 bjh21 break; /* last byte processed, report changes */
230 1.1 bjh21 }
231 1.1 bjh21
232 1.1 bjh21 any = 0;
233 1.1 bjh21 get = ms->ms_events.ev_get;
234 1.1 bjh21 put = ms->ms_events.ev_put;
235 1.1 bjh21 fe = &ms->ms_events.ev_q[put];
236 1.1 bjh21
237 1.1 bjh21 /* NEXT prepares to put the next event, backing off if necessary */
238 1.1 bjh21 #define NEXT do { \
239 1.1 bjh21 if ((++put) % EV_QSIZE == get) { \
240 1.1 bjh21 --put; \
241 1.1 bjh21 goto out; \
242 1.1 bjh21 } \
243 1.1 bjh21 } while (0)
244 1.1 bjh21
245 1.1 bjh21 /* ADVANCE completes the `put' of the event */
246 1.1 bjh21 #define ADVANCE do { \
247 1.1 bjh21 ++fe; \
248 1.1 bjh21 if (put >= EV_QSIZE) { \
249 1.1 bjh21 put = 0; \
250 1.1 bjh21 fe = &ms->ms_events.ev_q[0]; \
251 1.1 bjh21 } \
252 1.1 bjh21 any = 1; \
253 1.1 bjh21 } while (0)
254 1.1 bjh21
255 1.1 bjh21 ub = ms->ms_ub; /* old buttons state */
256 1.1 bjh21 mb = ms->ms_mb; /* new buttons state */
257 1.1 bjh21 while ((d = mb ^ ub) != 0) {
258 1.1 bjh21 /*
259 1.1 bjh21 * Mouse button change. Convert up to three state changes
260 1.1 bjh21 * to the `first' change, and drop it into the event queue.
261 1.1 bjh21 */
262 1.1 bjh21 NEXT;
263 1.1 bjh21 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
264 1.1 bjh21 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
265 1.1 bjh21 fe->value = (mb & d) ? VKEY_DOWN : VKEY_UP;
266 1.3 kardel getmicrotime(&fe->time);
267 1.1 bjh21 ADVANCE;
268 1.1 bjh21 ub ^= d; /* reflect the button state change */
269 1.1 bjh21 }
270 1.1 bjh21
271 1.1 bjh21 if (ms->ms_dx != 0) {
272 1.1 bjh21 NEXT;
273 1.1 bjh21 fe->id = LOC_X_DELTA;
274 1.1 bjh21 fe->value = ms->ms_dx;
275 1.3 kardel getmicrotime(&fe->time);
276 1.1 bjh21 ADVANCE;
277 1.1 bjh21 ms->ms_dx = 0;
278 1.1 bjh21 }
279 1.1 bjh21
280 1.1 bjh21 if (ms->ms_dy != 0) {
281 1.1 bjh21 NEXT;
282 1.1 bjh21 fe->id = LOC_Y_DELTA;
283 1.1 bjh21 fe->value = ms->ms_dy;
284 1.3 kardel getmicrotime(&fe->time);
285 1.1 bjh21 ADVANCE;
286 1.1 bjh21 ms->ms_dy = 0;
287 1.1 bjh21 }
288 1.1 bjh21
289 1.1 bjh21 out:
290 1.1 bjh21 if (any) {
291 1.1 bjh21 ms->ms_ub = ub; /* save button state */
292 1.1 bjh21 ms->ms_events.ev_put = put;
293 1.1 bjh21 EV_WAKEUP(&ms->ms_events);
294 1.1 bjh21 }
295 1.1 bjh21 }
296