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