ms_pckbport.c revision 1.1 1 1.1 bjh21 /* $NetBSD: ms_pckbport.c,v 1.1 2004/03/13 17:31:33 bjh21 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.1 bjh21 __KERNEL_RCSID(0, "$NetBSD: ms_pckbport.c,v 1.1 2004/03/13 17:31:33 bjh21 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/malloc.h>
43 1.1 bjh21 #include <sys/select.h>
44 1.1 bjh21 #include <sys/proc.h>
45 1.1 bjh21 #include <sys/signalvar.h>
46 1.1 bjh21
47 1.1 bjh21 #include <machine/autoconf.h>
48 1.1 bjh21 #include <machine/bus.h>
49 1.1 bjh21 #include <machine/intr.h>
50 1.1 bjh21
51 1.1 bjh21 #include <dev/pckbport/pckbportvar.h>
52 1.1 bjh21 #include <dev/pckbport/pmsreg.h>
53 1.1 bjh21
54 1.1 bjh21 #include <machine/vuid_event.h>
55 1.1 bjh21 #include <dev/sun/event_var.h>
56 1.1 bjh21 #include <dev/sun/msvar.h>
57 1.1 bjh21
58 1.1 bjh21 /*
59 1.1 bjh21 * NB: we {re,ab}use ms_softc input translator state and ignore its
60 1.1 bjh21 * zs-related members. Not quite clean, but what the heck.
61 1.1 bjh21 */
62 1.1 bjh21 struct ms_pckbport_softc {
63 1.1 bjh21 struct ms_softc sc_ms;
64 1.1 bjh21
65 1.1 bjh21 /* pckbport attachment */
66 1.1 bjh21 pckbport_tag_t sc_kbctag;
67 1.1 bjh21 pckbport_slot_t sc_kbcslot;
68 1.1 bjh21
69 1.1 bjh21 int sc_enabled; /* input enabled? */
70 1.1 bjh21 };
71 1.1 bjh21
72 1.1 bjh21 static int ms_pckbport_match(struct device *, struct cfdata *, void *);
73 1.1 bjh21 static void ms_pckbport_attach(struct device *, struct device *, void *);
74 1.1 bjh21
75 1.1 bjh21 CFATTACH_DECL(ms_pckbport, sizeof(struct ms_pckbport_softc),
76 1.1 bjh21 ms_pckbport_match, ms_pckbport_attach, NULL, NULL);
77 1.1 bjh21
78 1.1 bjh21
79 1.1 bjh21 static int ms_pckbport_iopen(struct device *, int);
80 1.1 bjh21 static int ms_pckbport_iclose(struct device *, int);
81 1.1 bjh21 static void ms_pckbport_input(void *, int);
82 1.1 bjh21
83 1.1 bjh21
84 1.1 bjh21 static int
85 1.1 bjh21 ms_pckbport_match(parent, cf, aux)
86 1.1 bjh21 struct device *parent;
87 1.1 bjh21 struct cfdata *cf;
88 1.1 bjh21 void *aux;
89 1.1 bjh21 {
90 1.1 bjh21 struct pckbport_attach_args *pa = aux;
91 1.1 bjh21
92 1.1 bjh21 return (pa->pa_slot == PCKBPORT_AUX_SLOT);
93 1.1 bjh21 }
94 1.1 bjh21
95 1.1 bjh21
96 1.1 bjh21 static void
97 1.1 bjh21 ms_pckbport_attach(parent, self, aux)
98 1.1 bjh21 struct device *parent, *self;
99 1.1 bjh21 void *aux;
100 1.1 bjh21 {
101 1.1 bjh21 struct ms_pckbport_softc *sc = (struct ms_pckbport_softc *)self;
102 1.1 bjh21 struct ms_softc *ms = &sc->sc_ms;
103 1.1 bjh21 struct pckbport_attach_args *pa = aux;
104 1.1 bjh21
105 1.1 bjh21 u_char cmd[1], resp[2];
106 1.1 bjh21 int res;
107 1.1 bjh21
108 1.1 bjh21 /* save our pckbport attachment */
109 1.1 bjh21 sc->sc_kbctag = pa->pa_tag;
110 1.1 bjh21 sc->sc_kbcslot = pa->pa_slot;
111 1.1 bjh21
112 1.1 bjh21 /* Hooks called by upper layer on device open/close */
113 1.1 bjh21 ms->ms_deviopen = ms_pckbport_iopen;
114 1.1 bjh21 ms->ms_deviclose = ms_pckbport_iclose;
115 1.1 bjh21
116 1.1 bjh21 printf("\n");
117 1.1 bjh21
118 1.1 bjh21 /* reset the device */
119 1.1 bjh21 cmd[0] = PMS_RESET;
120 1.1 bjh21 res = pckbport_poll_cmd(sc->sc_kbctag, sc->sc_kbcslot,
121 1.1 bjh21 cmd, 1, 2, resp, 1);
122 1.1 bjh21 #ifdef DIAGNOSTIC
123 1.1 bjh21 if (res || resp[0] != PMS_RSTDONE || resp[1] != 0) {
124 1.1 bjh21 printf("ms_pckbport_attach: reset error\n");
125 1.1 bjh21 /* return; */
126 1.1 bjh21 }
127 1.1 bjh21 #endif
128 1.1 bjh21
129 1.1 bjh21 pckbport_set_inputhandler(sc->sc_kbctag, sc->sc_kbcslot,
130 1.1 bjh21 ms_pckbport_input, sc, ms->ms_dev.dv_xname);
131 1.1 bjh21
132 1.1 bjh21 /* no interrupts until device is actually opened */
133 1.1 bjh21 cmd[0] = PMS_DEV_DISABLE;
134 1.1 bjh21 res = pckbport_poll_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
135 1.1 bjh21 1, 0, 0, 0);
136 1.1 bjh21 if (res)
137 1.1 bjh21 printf("ms_pckbport_attach: failed to disable interrupts\n");
138 1.1 bjh21 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
139 1.1 bjh21 }
140 1.1 bjh21
141 1.1 bjh21
142 1.1 bjh21 static int
143 1.1 bjh21 ms_pckbport_iopen(self, flags)
144 1.1 bjh21 struct device *self;
145 1.1 bjh21 int flags;
146 1.1 bjh21 {
147 1.1 bjh21 struct ms_pckbport_softc *sc = (struct ms_pckbport_softc *)self;
148 1.1 bjh21 struct ms_softc *ms = &sc->sc_ms;
149 1.1 bjh21 u_char cmd[1];
150 1.1 bjh21 int res;
151 1.1 bjh21
152 1.1 bjh21 ms->ms_byteno = 0;
153 1.1 bjh21 ms->ms_dx = ms->ms_dy = 0;
154 1.1 bjh21 ms->ms_ub = ms->ms_mb = 0;
155 1.1 bjh21
156 1.1 bjh21 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 1);
157 1.1 bjh21
158 1.1 bjh21 cmd[0] = PMS_DEV_ENABLE;
159 1.1 bjh21 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot,
160 1.1 bjh21 cmd, 1, 0, 1, NULL);
161 1.1 bjh21 if (res) {
162 1.1 bjh21 printf("pms_enable: command error\n");
163 1.1 bjh21 return (res);
164 1.1 bjh21 }
165 1.1 bjh21
166 1.1 bjh21 sc->sc_enabled = 1;
167 1.1 bjh21 return (0);
168 1.1 bjh21 }
169 1.1 bjh21
170 1.1 bjh21
171 1.1 bjh21 static int
172 1.1 bjh21 ms_pckbport_iclose(self, flags)
173 1.1 bjh21 struct device *self;
174 1.1 bjh21 int flags;
175 1.1 bjh21 {
176 1.1 bjh21 struct ms_pckbport_softc *sc = (struct ms_pckbport_softc *)self;
177 1.1 bjh21 u_char cmd[1];
178 1.1 bjh21 int res;
179 1.1 bjh21
180 1.1 bjh21 cmd[0] = PMS_DEV_DISABLE;
181 1.1 bjh21 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot,
182 1.1 bjh21 cmd, 1, 0, 1, NULL);
183 1.1 bjh21 if (res)
184 1.1 bjh21 printf("pms_disable: command error\n");
185 1.1 bjh21
186 1.1 bjh21 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
187 1.1 bjh21
188 1.1 bjh21 sc->sc_enabled = 0;
189 1.1 bjh21 return (0);
190 1.1 bjh21 }
191 1.1 bjh21
192 1.1 bjh21
193 1.1 bjh21 /* Masks for the first byte of a PS/2 mouse packet */
194 1.1 bjh21 #define PS2LBUTMASK 0x01
195 1.1 bjh21 #define PS2RBUTMASK 0x02
196 1.1 bjh21 #define PS2MBUTMASK 0x04
197 1.1 bjh21
198 1.1 bjh21 /*
199 1.1 bjh21 * Got a receive interrupt - pckbport wants to give us a byte.
200 1.1 bjh21 */
201 1.1 bjh21 static void
202 1.1 bjh21 ms_pckbport_input(vsc, data)
203 1.1 bjh21 void *vsc;
204 1.1 bjh21 int data;
205 1.1 bjh21 {
206 1.1 bjh21 struct ms_pckbport_softc *sc = vsc;
207 1.1 bjh21 struct ms_softc *ms = &sc->sc_ms;
208 1.1 bjh21 struct firm_event *fe;
209 1.1 bjh21 int mb, ub, d, get, put, any;
210 1.1 bjh21
211 1.1 bjh21 /* map changed buttons mask to the highest bit */
212 1.1 bjh21 static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
213 1.1 bjh21
214 1.1 bjh21 /* map bits to mouse buttons */
215 1.1 bjh21 static const int to_id[] = { MS_LEFT, MS_MIDDLE, 0, MS_RIGHT };
216 1.1 bjh21
217 1.1 bjh21 if (!sc->sc_enabled) {
218 1.1 bjh21 /* Interrupts are not expected. Discard the byte. */
219 1.1 bjh21 return;
220 1.1 bjh21 }
221 1.1 bjh21
222 1.1 bjh21 switch (ms->ms_byteno) {
223 1.1 bjh21
224 1.1 bjh21 case 0:
225 1.1 bjh21 if ((data & 0xc0) == 0) { /* no ovfl, bit 3 == 1 too? */
226 1.1 bjh21 ms->ms_mb =
227 1.1 bjh21 ((data & PS2LBUTMASK) ? 0x1 : 0) |
228 1.1 bjh21 ((data & PS2MBUTMASK) ? 0x2 : 0) |
229 1.1 bjh21 ((data & PS2RBUTMASK) ? 0x4 : 0) ;
230 1.1 bjh21 ++ms->ms_byteno;
231 1.1 bjh21 }
232 1.1 bjh21 return;
233 1.1 bjh21
234 1.1 bjh21 case 1:
235 1.1 bjh21 ms->ms_dx += (int8_t)data;
236 1.1 bjh21 ++ms->ms_byteno;
237 1.1 bjh21 return;
238 1.1 bjh21
239 1.1 bjh21 case 2:
240 1.1 bjh21 ms->ms_dy += (int8_t)data;
241 1.1 bjh21 ms->ms_byteno = 0;
242 1.1 bjh21 break; /* last byte processed, report changes */
243 1.1 bjh21 }
244 1.1 bjh21
245 1.1 bjh21 any = 0;
246 1.1 bjh21 get = ms->ms_events.ev_get;
247 1.1 bjh21 put = ms->ms_events.ev_put;
248 1.1 bjh21 fe = &ms->ms_events.ev_q[put];
249 1.1 bjh21
250 1.1 bjh21 /* NEXT prepares to put the next event, backing off if necessary */
251 1.1 bjh21 #define NEXT do { \
252 1.1 bjh21 if ((++put) % EV_QSIZE == get) { \
253 1.1 bjh21 --put; \
254 1.1 bjh21 goto out; \
255 1.1 bjh21 } \
256 1.1 bjh21 } while (0)
257 1.1 bjh21
258 1.1 bjh21 /* ADVANCE completes the `put' of the event */
259 1.1 bjh21 #define ADVANCE do { \
260 1.1 bjh21 ++fe; \
261 1.1 bjh21 if (put >= EV_QSIZE) { \
262 1.1 bjh21 put = 0; \
263 1.1 bjh21 fe = &ms->ms_events.ev_q[0]; \
264 1.1 bjh21 } \
265 1.1 bjh21 any = 1; \
266 1.1 bjh21 } while (0)
267 1.1 bjh21
268 1.1 bjh21 ub = ms->ms_ub; /* old buttons state */
269 1.1 bjh21 mb = ms->ms_mb; /* new buttons state */
270 1.1 bjh21 while ((d = mb ^ ub) != 0) {
271 1.1 bjh21 /*
272 1.1 bjh21 * Mouse button change. Convert up to three state changes
273 1.1 bjh21 * to the `first' change, and drop it into the event queue.
274 1.1 bjh21 */
275 1.1 bjh21 NEXT;
276 1.1 bjh21 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
277 1.1 bjh21 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
278 1.1 bjh21 fe->value = (mb & d) ? VKEY_DOWN : VKEY_UP;
279 1.1 bjh21 fe->time = time;
280 1.1 bjh21 ADVANCE;
281 1.1 bjh21 ub ^= d; /* reflect the button state change */
282 1.1 bjh21 }
283 1.1 bjh21
284 1.1 bjh21 if (ms->ms_dx != 0) {
285 1.1 bjh21 NEXT;
286 1.1 bjh21 fe->id = LOC_X_DELTA;
287 1.1 bjh21 fe->value = ms->ms_dx;
288 1.1 bjh21 fe->time = time;
289 1.1 bjh21 ADVANCE;
290 1.1 bjh21 ms->ms_dx = 0;
291 1.1 bjh21 }
292 1.1 bjh21
293 1.1 bjh21 if (ms->ms_dy != 0) {
294 1.1 bjh21 NEXT;
295 1.1 bjh21 fe->id = LOC_Y_DELTA;
296 1.1 bjh21 fe->value = ms->ms_dy;
297 1.1 bjh21 fe->time = time;
298 1.1 bjh21 ADVANCE;
299 1.1 bjh21 ms->ms_dy = 0;
300 1.1 bjh21 }
301 1.1 bjh21
302 1.1 bjh21 out:
303 1.1 bjh21 if (any) {
304 1.1 bjh21 ms->ms_ub = ub; /* save button state */
305 1.1 bjh21 ms->ms_events.ev_put = put;
306 1.1 bjh21 EV_WAKEUP(&ms->ms_events);
307 1.1 bjh21 }
308 1.1 bjh21 }
309