1 /* $NetBSD: opms.c,v 1.17 2008/03/01 14:16:49 rmind Exp $ */
2 /* $OpenBSD: pccons.c,v 1.22 1999/01/30 22:39:37 imp Exp $ */
3 /* NetBSD: pms.c,v 1.21 1995/04/18 02:25:18 mycroft Exp */
6 * Copyright (c) 1990 The Regents of the University of California.
9 * This code is derived from software contributed to Berkeley by
10 * William Jolitz and Don Ahn.
12 * Copyright (c) 1994 Charles M. Hannum.
13 * Copyright (c) 1992, 1993 Erik Forsberg.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)pccons.c 5.11 (Berkeley) 5/21/91
43 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
45 * This code is derived from software contributed to Berkeley by
46 * William Jolitz and Don Ahn.
48 * Copyright (c) 1994 Charles M. Hannum.
49 * Copyright (c) 1992, 1993 Erik Forsberg.
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. All advertising materials mentioning features or use of this software
60 * must display the following acknowledgement:
61 * This product includes software developed by the University of
62 * California, Berkeley and its contributors.
63 * 4. Neither the name of the University nor the names of its contributors
64 * may be used to endorse or promote products derived from this software
65 * without specific prior written permission.
67 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
79 * @(#)pccons.c 5.11 (Berkeley) 5/21/91
82 #include <sys/cdefs.h>
83 __KERNEL_RCSID(0, "$NetBSD: opms.c,v 1.17 2008/03/01 14:16:49 rmind Exp $");
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/vnode.h>
90 #include <sys/device.h>
94 #include <machine/bus.h>
95 #include <machine/kbdreg.h>
96 #include <machine/mouse.h>
98 #include <arc/dev/pcconsvar.h>
99 #include <arc/dev/opmsvar.h>
103 #define PMSUNIT(dev) (minor(dev))
106 #define PMS_OBUF_FULL 0x01
107 #define PMS_IBUF_FULL 0x02
109 /* controller commands */
110 #define PMS_INT_ENABLE 0x47 /* enable controller interrupts */
111 #define PMS_INT_DISABLE 0x65 /* disable controller interrupts */
112 #define PMS_AUX_ENABLE 0xa7 /* enable auxiliary port */
113 #define PMS_AUX_DISABLE 0xa8 /* disable auxiliary port */
114 #define PMS_MAGIC_1 0xa9 /* XXX */
116 #define PMS_8042_CMD 0x65
119 #define PMS_SET_SCALE11 0xe6 /* set scaling 1:1 */
120 #define PMS_SET_SCALE21 0xe7 /* set scaling 2:1 */
121 #define PMS_SET_RES 0xe8 /* set resolution */
122 #define PMS_GET_SCALE 0xe9 /* get scaling factor */
123 #define PMS_SET_STREAM 0xea /* set streaming mode */
124 #define PMS_SET_SAMPLE 0xf3 /* set sampling rate */
125 #define PMS_DEV_ENABLE 0xf4 /* mouse on */
126 #define PMS_DEV_DISABLE 0xf5 /* mouse off */
127 #define PMS_RESET 0xff /* reset */
129 #define PMS_CHUNK 128 /* chunk size for read */
130 #define PMS_BSIZE 1020 /* buffer size */
132 #define FLUSHQ(q) { if((q)->c_cc) ndflush(q, (q)->c_cc); }
134 dev_type_open(opmsopen
);
135 dev_type_close(opmsclose
);
136 dev_type_read(opmsread
);
137 dev_type_ioctl(opmsioctl
);
138 dev_type_poll(opmspoll
);
139 dev_type_kqfilter(opmskqfilter
);
141 const struct cdevsw opms_cdevsw
= {
142 opmsopen
, opmsclose
, opmsread
, nowrite
, opmsioctl
,
143 nostop
, notty
, opmspoll
, nommap
, opmskqfilter
,
146 static inline void pms_dev_cmd(uint8_t);
147 static inline void pms_aux_cmd(uint8_t);
148 static inline void pms_pit_cmd(uint8_t);
151 pms_dev_cmd(uint8_t value
)
155 kbd_cmd_write_1(0xd4);
157 kbd_data_write_1(value
);
161 pms_aux_cmd(uint8_t value
)
165 kbd_cmd_write_1(value
);
169 pms_pit_cmd(uint8_t value
)
173 kbd_cmd_write_1(0x60);
175 kbd_data_write_1(value
);
178 int opms_common_match(bus_space_tag_t kbd_iot
, struct pccons_config
*config
)
182 kbd_context_init(kbd_iot
, config
);
184 pms_dev_cmd(KBC_RESET
);
185 pms_aux_cmd(PMS_MAGIC_1
);
187 x
= kbd_data_read_1();
188 pms_pit_cmd(PMS_INT_DISABLE
);
196 opms_common_attach(struct opms_softc
*sc
, bus_space_tag_t opms_iot
,
197 struct pccons_config
*config
)
200 kbd_context_init(opms_iot
, config
);
201 selinit(&sc
->sc_rsel
);
203 /* Other initialization was done by opmsprobe. */
208 opmsopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
210 struct opms_softc
*sc
;
212 sc
= device_lookup_private(&opms_cd
, PMSUNIT(dev
));
216 if (sc
->sc_state
& PMS_OPEN
)
219 if (clalloc(&sc
->sc_q
, PMS_BSIZE
, 0) == -1)
222 sc
->sc_state
|= PMS_OPEN
;
224 sc
->sc_x
= sc
->sc_y
= 0;
226 /* Enable interrupts. */
227 pms_dev_cmd(PMS_DEV_ENABLE
);
228 pms_aux_cmd(PMS_AUX_ENABLE
);
229 pms_dev_cmd(PMS_SET_RES
);
230 pms_dev_cmd(3); /* 8 counts/mm */
231 pms_dev_cmd(PMS_SET_SCALE21
);
233 pms_dev_cmd(PMS_SET_SAMPLE
);
234 pms_dev_cmd(100); /* 100 samples/sec */
235 pms_dev_cmd(PMS_SET_STREAM
);
237 pms_pit_cmd(PMS_INT_ENABLE
);
243 opmsclose(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
245 struct opms_softc
*sc
= device_lookup_private(&opms_cd
, PMSUNIT(dev
));
247 /* Disable interrupts. */
248 pms_dev_cmd(PMS_DEV_DISABLE
);
249 pms_pit_cmd(PMS_INT_DISABLE
);
250 pms_aux_cmd(PMS_AUX_DISABLE
);
252 sc
->sc_state
&= ~PMS_OPEN
;
260 opmsread(dev_t dev
, struct uio
*uio
, int flag
)
262 struct opms_softc
*sc
= device_lookup_private(&opms_cd
, PMSUNIT(dev
));
266 u_char buffer
[PMS_CHUNK
];
268 /* Block until mouse activity occurred. */
271 while (sc
->sc_q
.c_cc
== 0) {
272 if (flag
& IO_NDELAY
) {
276 sc
->sc_state
|= PMS_ASLP
;
277 error
= tsleep((void *)sc
, PZERO
| PCATCH
, "pmsrea", 0);
279 sc
->sc_state
&= ~PMS_ASLP
;
286 /* Transfer as many chunks as possible. */
288 while (sc
->sc_q
.c_cc
> 0 && uio
->uio_resid
> 0) {
289 length
= min(sc
->sc_q
.c_cc
, uio
->uio_resid
);
290 if (length
> sizeof(buffer
))
291 length
= sizeof(buffer
);
293 /* Remove a small chunk from the input queue. */
294 (void) q_to_b(&sc
->sc_q
, buffer
, length
);
296 /* Copy the data to the user process. */
297 error
= uiomove(buffer
, length
, uio
);
306 opmsioctl(dev_t dev
, u_long cmd
, void *addr
, int flag
, struct lwp
*l
)
308 struct opms_softc
*sc
= device_lookup_private(&opms_cd
, PMSUNIT(dev
));
309 struct mouseinfo info
;
317 info
.status
= sc
->sc_status
;
318 if (sc
->sc_x
|| sc
->sc_y
)
319 info
.status
|= MOVEMENT
;
323 else if (sc
->sc_x
< -127)
324 /* Bounding at -127 avoids a bug in XFree86. */
327 info
.xmotion
= sc
->sc_x
;
331 else if (sc
->sc_y
< -127)
334 info
.ymotion
= sc
->sc_y
;
336 /* Reset historical information. */
337 sc
->sc_x
= sc
->sc_y
= 0;
338 sc
->sc_status
&= ~BUTCHNGMASK
;
339 ndflush(&sc
->sc_q
, sc
->sc_q
.c_cc
);
342 error
= copyout(&info
, addr
, sizeof(struct mouseinfo
));
352 /* Masks for the first byte of a packet */
353 #define PS2LBUTMASK 0x01
354 #define PS2RBUTMASK 0x02
355 #define PS2MBUTMASK 0x04
360 struct opms_softc
*sc
= arg
;
361 static int state
= 0;
362 static u_char buttons
;
367 if ((sc
->sc_state
& PMS_OPEN
) == 0) {
368 /* Interrupts are not expected. Discard the byte. */
376 buttons
= kbd_data_read_1();
377 if ((buttons
& 0xc0) == 0)
382 dx
= kbd_data_read_1();
383 /* Bounding at -127 avoids a bug in XFree86. */
384 dx
= (dx
== -128) ? -127 : dx
;
389 dy
= kbd_data_read_1();
390 dy
= (dy
== -128) ? -127 : dy
;
393 buttons
= ((buttons
& PS2LBUTMASK
) << 2) |
394 ((buttons
& (PS2RBUTMASK
| PS2MBUTMASK
)) >> 1);
395 changed
= ((buttons
^ sc
->sc_status
) & BUTSTATMASK
) << 3;
396 sc
->sc_status
= buttons
| (sc
->sc_status
& ~BUTSTATMASK
) |
399 if (dx
|| dy
|| changed
) {
400 /* Update accumulated movements. */
404 /* Add this event to the queue. */
405 buffer
[0] = 0x80 | (buttons
& BUTSTATMASK
);
408 buffer
[1] = dx
& 0x7f;
411 buffer
[2] = dy
& 0x7f;
412 buffer
[3] = buffer
[4] = 0;
413 (void) b_to_q(buffer
, sizeof buffer
, &sc
->sc_q
);
415 if (sc
->sc_state
& PMS_ASLP
) {
416 sc
->sc_state
&= ~PMS_ASLP
;
419 selnotify(&sc
->sc_rsel
, 0, 0);
428 opmspoll(dev_t dev
, int events
, struct lwp
*l
)
430 struct opms_softc
*sc
= device_lookup_private(&opms_cd
, PMSUNIT(dev
));
434 if (events
& (POLLIN
| POLLRDNORM
)) {
435 if (sc
->sc_q
.c_cc
> 0)
436 revents
|= events
& (POLLIN
| POLLRDNORM
);
438 selrecord(l
, &sc
->sc_rsel
);
446 filt_opmsrdetach(struct knote
*kn
)
448 struct opms_softc
*sc
= kn
->kn_hook
;
452 SLIST_REMOVE(&sc
->sc_rsel
.sel_klist
, kn
, knote
, kn_selnext
);
457 filt_opmsread(struct knote
*kn
, long hint
)
459 struct opms_softc
*sc
= kn
->kn_hook
;
461 kn
->kn_data
= sc
->sc_q
.c_cc
;
462 return kn
->kn_data
> 0;
465 static const struct filterops opmsread_filtops
=
466 { 1, NULL
, filt_opmsrdetach
, filt_opmsread
};
469 opmskqfilter(dev_t dev
, struct knote
*kn
)
471 struct opms_softc
*sc
= device_lookup_private(&opms_cd
, PMSUNIT(dev
));
475 switch (kn
->kn_filter
) {
477 klist
= &sc
->sc_rsel
.sel_klist
;
478 kn
->kn_fop
= &opmsread_filtops
;
488 SLIST_INSERT_HEAD(klist
, kn
, kn_selnext
);