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