1 /* $NetBSD: ms_pckbport.c,v 1.4 2008/04/07 13:31:15 tsutsui Exp $ */
4 * Copyright (c) 2002 Valeriy E. Ushakov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ms_pckbport.c,v 1.4 2008/04/07 13:31:15 tsutsui Exp $");
33 * Attach PS/2 mouse at pckbport aux port
34 * and convert PS/2 mouse protocol to Sun firm events.
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/kernel.h>
42 #include <sys/select.h>
45 #include <machine/autoconf.h>
46 #include <machine/bus.h>
47 #include <machine/intr.h>
49 #include <dev/pckbport/pckbportvar.h>
50 #include <dev/pckbport/pmsreg.h>
52 #include <machine/vuid_event.h>
53 #include <dev/sun/event_var.h>
54 #include <dev/sun/msvar.h>
57 * NB: we {re,ab}use ms_softc input translator state and ignore its
58 * zs-related members. Not quite clean, but what the heck.
60 struct ms_pckbport_softc
{
61 struct ms_softc sc_ms
;
63 /* pckbport attachment */
64 pckbport_tag_t sc_kbctag
;
65 pckbport_slot_t sc_kbcslot
;
67 int sc_enabled
; /* input enabled? */
70 static int ms_pckbport_match(device_t
, cfdata_t
, void *);
71 static void ms_pckbport_attach(device_t
, device_t
, void *);
73 CFATTACH_DECL_NEW(ms_pckbport
, sizeof(struct ms_pckbport_softc
),
74 ms_pckbport_match
, ms_pckbport_attach
, NULL
, NULL
);
77 static int ms_pckbport_iopen(struct device
*, int);
78 static int ms_pckbport_iclose(struct device
*, int);
79 static void ms_pckbport_input(void *, int);
83 ms_pckbport_match(device_t parent
, cfdata_t cf
, void *aux
)
85 struct pckbport_attach_args
*pa
= aux
;
87 return (pa
->pa_slot
== PCKBPORT_AUX_SLOT
);
92 ms_pckbport_attach(device_t parent
, device_t self
, void *aux
)
94 struct ms_pckbport_softc
*sc
= device_private(self
);
95 struct ms_softc
*ms
= &sc
->sc_ms
;
96 struct pckbport_attach_args
*pa
= aux
;
98 u_char cmd
[1], resp
[2];
103 /* save our pckbport attachment */
104 sc
->sc_kbctag
= pa
->pa_tag
;
105 sc
->sc_kbcslot
= pa
->pa_slot
;
107 /* Hooks called by upper layer on device open/close */
108 ms
->ms_deviopen
= ms_pckbport_iopen
;
109 ms
->ms_deviclose
= ms_pckbport_iclose
;
113 /* reset the device */
115 res
= pckbport_poll_cmd(sc
->sc_kbctag
, sc
->sc_kbcslot
,
118 if (res
|| resp
[0] != PMS_RSTDONE
|| resp
[1] != 0) {
119 aprint_error("%s: reset error\n", __func__
);
124 pckbport_set_inputhandler(sc
->sc_kbctag
, sc
->sc_kbcslot
,
125 ms_pckbport_input
, sc
, device_xname(self
));
127 /* no interrupts until device is actually opened */
128 cmd
[0] = PMS_DEV_DISABLE
;
129 res
= pckbport_poll_cmd(sc
->sc_kbctag
, sc
->sc_kbcslot
, cmd
,
132 aprint_error("%s: failed to disable interrupts\n", __func__
);
133 pckbport_slot_enable(sc
->sc_kbctag
, sc
->sc_kbcslot
, 0);
138 ms_pckbport_iopen(struct device
*self
, int flags
)
140 struct ms_pckbport_softc
*sc
= device_private(self
);
141 struct ms_softc
*ms
= &sc
->sc_ms
;
146 ms
->ms_dx
= ms
->ms_dy
= 0;
147 ms
->ms_ub
= ms
->ms_mb
= 0;
149 pckbport_slot_enable(sc
->sc_kbctag
, sc
->sc_kbcslot
, 1);
151 cmd
[0] = PMS_DEV_ENABLE
;
152 res
= pckbport_enqueue_cmd(sc
->sc_kbctag
, sc
->sc_kbcslot
,
155 printf("pms_enable: command error\n");
165 ms_pckbport_iclose(struct device
*self
, int flags
)
167 struct ms_pckbport_softc
*sc
= device_private(self
);
171 cmd
[0] = PMS_DEV_DISABLE
;
172 res
= pckbport_enqueue_cmd(sc
->sc_kbctag
, sc
->sc_kbcslot
,
175 printf("pms_disable: command error\n");
177 pckbport_slot_enable(sc
->sc_kbctag
, sc
->sc_kbcslot
, 0);
184 /* Masks for the first byte of a PS/2 mouse packet */
185 #define PS2LBUTMASK 0x01
186 #define PS2RBUTMASK 0x02
187 #define PS2MBUTMASK 0x04
190 * Got a receive interrupt - pckbport wants to give us a byte.
193 ms_pckbport_input(void *vsc
, int data
)
195 struct ms_pckbport_softc
*sc
= vsc
;
196 struct ms_softc
*ms
= &sc
->sc_ms
;
197 struct firm_event
*fe
;
198 int mb
, ub
, d
, get
, put
, any
;
200 /* map changed buttons mask to the highest bit */
201 static const char to_one
[] = { 1, 2, 2, 4, 4, 4, 4 };
203 /* map bits to mouse buttons */
204 static const int to_id
[] = { MS_LEFT
, MS_MIDDLE
, 0, MS_RIGHT
};
206 if (!sc
->sc_enabled
) {
207 /* Interrupts are not expected. Discard the byte. */
211 switch (ms
->ms_byteno
) {
214 if ((data
& 0xc0) == 0) { /* no ovfl, bit 3 == 1 too? */
216 ((data
& PS2LBUTMASK
) ? 0x1 : 0) |
217 ((data
& PS2MBUTMASK
) ? 0x2 : 0) |
218 ((data
& PS2RBUTMASK
) ? 0x4 : 0) ;
224 ms
->ms_dx
+= (int8_t)data
;
229 ms
->ms_dy
+= (int8_t)data
;
231 break; /* last byte processed, report changes */
235 get
= ms
->ms_events
.ev_get
;
236 put
= ms
->ms_events
.ev_put
;
237 fe
= &ms
->ms_events
.ev_q
[put
];
239 /* NEXT prepares to put the next event, backing off if necessary */
241 if ((++put) % EV_QSIZE == get) { \
247 /* ADVANCE completes the `put' of the event */
248 #define ADVANCE do { \
250 if (put >= EV_QSIZE) { \
252 fe = &ms->ms_events.ev_q[0]; \
257 ub
= ms
->ms_ub
; /* old buttons state */
258 mb
= ms
->ms_mb
; /* new buttons state */
259 while ((d
= mb
^ ub
) != 0) {
261 * Mouse button change. Convert up to three state changes
262 * to the `first' change, and drop it into the event queue.
265 d
= to_one
[d
- 1]; /* from 1..7 to {1,2,4} */
266 fe
->id
= to_id
[d
- 1]; /* from {1,2,4} to ID */
267 fe
->value
= (mb
& d
) ? VKEY_DOWN
: VKEY_UP
;
270 ub
^= d
; /* reflect the button state change */
273 if (ms
->ms_dx
!= 0) {
275 fe
->id
= LOC_X_DELTA
;
276 fe
->value
= ms
->ms_dx
;
282 if (ms
->ms_dy
!= 0) {
284 fe
->id
= LOC_Y_DELTA
;
285 fe
->value
= ms
->ms_dy
;
293 ms
->ms_ub
= ub
; /* save button state */
294 ms
->ms_events
.ev_put
= put
;
295 EV_WAKEUP(&ms
->ms_events
);