Home | History | Annotate | Line # | Download | only in dev
opms.c revision 1.3.4.2
      1  1.3.4.2  nathanw /*	$NetBSD: opms.c,v 1.3.4.2 2002/09/17 21:13:15 nathanw Exp $	*/
      2  1.3.4.2  nathanw /*	$OpenBSD: pccons.c,v 1.22 1999/01/30 22:39:37 imp Exp $	*/
      3  1.3.4.2  nathanw /*	NetBSD: pms.c,v 1.21 1995/04/18 02:25:18 mycroft Exp	*/
      4  1.3.4.2  nathanw 
      5  1.3.4.2  nathanw /*-
      6  1.3.4.2  nathanw  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.  All rights reserved.
      7  1.3.4.2  nathanw  * Copyright (c) 1990 The Regents of the University of California.
      8  1.3.4.2  nathanw  * All rights reserved.
      9  1.3.4.2  nathanw  *
     10  1.3.4.2  nathanw  * This code is derived from software contributed to Berkeley by
     11  1.3.4.2  nathanw  * William Jolitz and Don Ahn.
     12  1.3.4.2  nathanw  *
     13  1.3.4.2  nathanw  * Copyright (c) 1994 Charles M. Hannum.
     14  1.3.4.2  nathanw  * Copyright (c) 1992, 1993 Erik Forsberg.
     15  1.3.4.2  nathanw  *
     16  1.3.4.2  nathanw  * Redistribution and use in source and binary forms, with or without
     17  1.3.4.2  nathanw  * modification, are permitted provided that the following conditions
     18  1.3.4.2  nathanw  * are met:
     19  1.3.4.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     20  1.3.4.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     21  1.3.4.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.3.4.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     23  1.3.4.2  nathanw  *    documentation and/or other materials provided with the distribution.
     24  1.3.4.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     25  1.3.4.2  nathanw  *    must display the following acknowledgement:
     26  1.3.4.2  nathanw  *	This product includes software developed by the University of
     27  1.3.4.2  nathanw  *	California, Berkeley and its contributors.
     28  1.3.4.2  nathanw  * 4. Neither the name of the University nor the names of its contributors
     29  1.3.4.2  nathanw  *    may be used to endorse or promote products derived from this software
     30  1.3.4.2  nathanw  *    without specific prior written permission.
     31  1.3.4.2  nathanw  *
     32  1.3.4.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  1.3.4.2  nathanw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  1.3.4.2  nathanw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  1.3.4.2  nathanw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  1.3.4.2  nathanw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  1.3.4.2  nathanw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  1.3.4.2  nathanw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  1.3.4.2  nathanw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  1.3.4.2  nathanw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  1.3.4.2  nathanw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  1.3.4.2  nathanw  * SUCH DAMAGE.
     43  1.3.4.2  nathanw  *
     44  1.3.4.2  nathanw  *	@(#)pccons.c	5.11 (Berkeley) 5/21/91
     45  1.3.4.2  nathanw  */
     46  1.3.4.2  nathanw 
     47  1.3.4.2  nathanw #include <sys/param.h>
     48  1.3.4.2  nathanw #include <sys/systm.h>
     49  1.3.4.2  nathanw #include <sys/vnode.h>
     50  1.3.4.2  nathanw #include <sys/poll.h>
     51  1.3.4.2  nathanw #include <sys/tty.h>
     52  1.3.4.2  nathanw #include <sys/device.h>
     53  1.3.4.2  nathanw #include <sys/proc.h>
     54  1.3.4.2  nathanw #include <sys/conf.h>
     55  1.3.4.2  nathanw 
     56  1.3.4.2  nathanw #include <machine/bus.h>
     57  1.3.4.2  nathanw #include <machine/kbdreg.h>
     58  1.3.4.2  nathanw #include <machine/mouse.h>
     59  1.3.4.2  nathanw 
     60  1.3.4.2  nathanw #include <arc/dev/pcconsvar.h>
     61  1.3.4.2  nathanw #include <arc/dev/opmsvar.h>
     62  1.3.4.2  nathanw 
     63  1.3.4.2  nathanw #define	PMSUNIT(dev)	(minor(dev))
     64  1.3.4.2  nathanw 
     65  1.3.4.2  nathanw /* status bits */
     66  1.3.4.2  nathanw #define	PMS_OBUF_FULL	0x01
     67  1.3.4.2  nathanw #define	PMS_IBUF_FULL	0x02
     68  1.3.4.2  nathanw 
     69  1.3.4.2  nathanw /* controller commands */
     70  1.3.4.2  nathanw #define	PMS_INT_ENABLE	0x47	/* enable controller interrupts */
     71  1.3.4.2  nathanw #define	PMS_INT_DISABLE	0x65	/* disable controller interrupts */
     72  1.3.4.2  nathanw #define	PMS_AUX_ENABLE	0xa7	/* enable auxiliary port */
     73  1.3.4.2  nathanw #define	PMS_AUX_DISABLE	0xa8	/* disable auxiliary port */
     74  1.3.4.2  nathanw #define	PMS_MAGIC_1	0xa9	/* XXX */
     75  1.3.4.2  nathanw 
     76  1.3.4.2  nathanw #define	PMS_8042_CMD	0x65
     77  1.3.4.2  nathanw 
     78  1.3.4.2  nathanw /* mouse commands */
     79  1.3.4.2  nathanw #define	PMS_SET_SCALE11	0xe6	/* set scaling 1:1 */
     80  1.3.4.2  nathanw #define	PMS_SET_SCALE21 0xe7	/* set scaling 2:1 */
     81  1.3.4.2  nathanw #define	PMS_SET_RES	0xe8	/* set resolution */
     82  1.3.4.2  nathanw #define	PMS_GET_SCALE	0xe9	/* get scaling factor */
     83  1.3.4.2  nathanw #define	PMS_SET_STREAM	0xea	/* set streaming mode */
     84  1.3.4.2  nathanw #define	PMS_SET_SAMPLE	0xf3	/* set sampling rate */
     85  1.3.4.2  nathanw #define	PMS_DEV_ENABLE	0xf4	/* mouse on */
     86  1.3.4.2  nathanw #define	PMS_DEV_DISABLE	0xf5	/* mouse off */
     87  1.3.4.2  nathanw #define	PMS_RESET	0xff	/* reset */
     88  1.3.4.2  nathanw 
     89  1.3.4.2  nathanw #define	PMS_CHUNK	128	/* chunk size for read */
     90  1.3.4.2  nathanw #define	PMS_BSIZE	1020	/* buffer size */
     91  1.3.4.2  nathanw 
     92  1.3.4.2  nathanw #define	FLUSHQ(q) { if((q)->c_cc) ndflush(q, (q)->c_cc); }
     93  1.3.4.2  nathanw 
     94  1.3.4.2  nathanw extern struct cfdriver opms_cd;
     95  1.3.4.2  nathanw 
     96  1.3.4.2  nathanw dev_type_open(opmsopen);
     97  1.3.4.2  nathanw dev_type_close(opmsclose);
     98  1.3.4.2  nathanw dev_type_read(opmsread);
     99  1.3.4.2  nathanw dev_type_ioctl(opmsioctl);
    100  1.3.4.2  nathanw dev_type_poll(opmspoll);
    101  1.3.4.2  nathanw 
    102  1.3.4.2  nathanw const struct cdevsw opms_cdevsw = {
    103  1.3.4.2  nathanw 	opmsopen, opmsclose, opmsread, nowrite, opmsioctl,
    104  1.3.4.2  nathanw 	nostop, notty, opmspoll, nommap,
    105  1.3.4.2  nathanw };
    106  1.3.4.2  nathanw 
    107  1.3.4.2  nathanw static __inline void pms_dev_cmd __P((u_char));
    108  1.3.4.2  nathanw static __inline void pms_aux_cmd __P((u_char));
    109  1.3.4.2  nathanw static __inline void pms_pit_cmd __P((u_char));
    110  1.3.4.2  nathanw 
    111  1.3.4.2  nathanw static __inline void
    112  1.3.4.2  nathanw pms_dev_cmd(value)
    113  1.3.4.2  nathanw 	u_char value;
    114  1.3.4.2  nathanw {
    115  1.3.4.2  nathanw 	kbd_flush_input();
    116  1.3.4.2  nathanw 	kbd_cmd_write_1(0xd4);
    117  1.3.4.2  nathanw 	kbd_flush_input();
    118  1.3.4.2  nathanw 	kbd_data_write_1(value);
    119  1.3.4.2  nathanw }
    120  1.3.4.2  nathanw 
    121  1.3.4.2  nathanw static __inline void
    122  1.3.4.2  nathanw pms_aux_cmd(value)
    123  1.3.4.2  nathanw 	u_char value;
    124  1.3.4.2  nathanw {
    125  1.3.4.2  nathanw 	kbd_flush_input();
    126  1.3.4.2  nathanw 	kbd_cmd_write_1(value);
    127  1.3.4.2  nathanw }
    128  1.3.4.2  nathanw 
    129  1.3.4.2  nathanw static __inline void
    130  1.3.4.2  nathanw pms_pit_cmd(value)
    131  1.3.4.2  nathanw 	u_char value;
    132  1.3.4.2  nathanw {
    133  1.3.4.2  nathanw 	kbd_flush_input();
    134  1.3.4.2  nathanw 	kbd_cmd_write_1(0x60);
    135  1.3.4.2  nathanw 	kbd_flush_input();
    136  1.3.4.2  nathanw 	kbd_data_write_1(value);
    137  1.3.4.2  nathanw }
    138  1.3.4.2  nathanw 
    139  1.3.4.2  nathanw int opms_common_match(kbd_iot, config)
    140  1.3.4.2  nathanw 	bus_space_tag_t kbd_iot;
    141  1.3.4.2  nathanw 	struct pccons_config *config;
    142  1.3.4.2  nathanw {
    143  1.3.4.2  nathanw 	u_char x;
    144  1.3.4.2  nathanw 
    145  1.3.4.2  nathanw 	kbd_context_init(kbd_iot, config);
    146  1.3.4.2  nathanw 
    147  1.3.4.2  nathanw 	pms_dev_cmd(KBC_RESET);
    148  1.3.4.2  nathanw 	pms_aux_cmd(PMS_MAGIC_1);
    149  1.3.4.2  nathanw 	delay(10000);
    150  1.3.4.2  nathanw 	x = kbd_data_read_1();
    151  1.3.4.2  nathanw 	pms_pit_cmd(PMS_INT_DISABLE);
    152  1.3.4.2  nathanw 	if (x & 0x04)
    153  1.3.4.2  nathanw 		return 0;
    154  1.3.4.2  nathanw 
    155  1.3.4.2  nathanw 	return 1;
    156  1.3.4.2  nathanw }
    157  1.3.4.2  nathanw 
    158  1.3.4.2  nathanw void
    159  1.3.4.2  nathanw opms_common_attach(sc, opms_iot, config)
    160  1.3.4.2  nathanw 	struct opms_softc *sc;
    161  1.3.4.2  nathanw 	bus_space_tag_t opms_iot;
    162  1.3.4.2  nathanw 	struct pccons_config *config;
    163  1.3.4.2  nathanw {
    164  1.3.4.2  nathanw 	kbd_context_init(opms_iot, config);
    165  1.3.4.2  nathanw 
    166  1.3.4.2  nathanw 	/* Other initialization was done by opmsprobe. */
    167  1.3.4.2  nathanw 	sc->sc_state = 0;
    168  1.3.4.2  nathanw }
    169  1.3.4.2  nathanw 
    170  1.3.4.2  nathanw int
    171  1.3.4.2  nathanw opmsopen(dev, flag, mode, p)
    172  1.3.4.2  nathanw 	dev_t dev;
    173  1.3.4.2  nathanw 	int flag, mode;
    174  1.3.4.2  nathanw 	struct proc *p;
    175  1.3.4.2  nathanw {
    176  1.3.4.2  nathanw 	int unit = PMSUNIT(dev);
    177  1.3.4.2  nathanw 	struct opms_softc *sc;
    178  1.3.4.2  nathanw 
    179  1.3.4.2  nathanw 	if (unit >= opms_cd.cd_ndevs)
    180  1.3.4.2  nathanw 		return ENXIO;
    181  1.3.4.2  nathanw 	sc = opms_cd.cd_devs[unit];
    182  1.3.4.2  nathanw 	if (!sc)
    183  1.3.4.2  nathanw 		return ENXIO;
    184  1.3.4.2  nathanw 
    185  1.3.4.2  nathanw 	if (sc->sc_state & PMS_OPEN)
    186  1.3.4.2  nathanw 		return EBUSY;
    187  1.3.4.2  nathanw 
    188  1.3.4.2  nathanw 	if (clalloc(&sc->sc_q, PMS_BSIZE, 0) == -1)
    189  1.3.4.2  nathanw 		return ENOMEM;
    190  1.3.4.2  nathanw 
    191  1.3.4.2  nathanw 	sc->sc_state |= PMS_OPEN;
    192  1.3.4.2  nathanw 	sc->sc_status = 0;
    193  1.3.4.2  nathanw 	sc->sc_x = sc->sc_y = 0;
    194  1.3.4.2  nathanw 
    195  1.3.4.2  nathanw 	/* Enable interrupts. */
    196  1.3.4.2  nathanw 	pms_dev_cmd(PMS_DEV_ENABLE);
    197  1.3.4.2  nathanw 	pms_aux_cmd(PMS_AUX_ENABLE);
    198  1.3.4.2  nathanw 	pms_dev_cmd(PMS_SET_RES);
    199  1.3.4.2  nathanw 	pms_dev_cmd(3);		/* 8 counts/mm */
    200  1.3.4.2  nathanw 	pms_dev_cmd(PMS_SET_SCALE21);
    201  1.3.4.2  nathanw #if 0
    202  1.3.4.2  nathanw 	pms_dev_cmd(PMS_SET_SAMPLE);
    203  1.3.4.2  nathanw 	pms_dev_cmd(100);	/* 100 samples/sec */
    204  1.3.4.2  nathanw 	pms_dev_cmd(PMS_SET_STREAM);
    205  1.3.4.2  nathanw #endif
    206  1.3.4.2  nathanw 	pms_pit_cmd(PMS_INT_ENABLE);
    207  1.3.4.2  nathanw 
    208  1.3.4.2  nathanw 	return 0;
    209  1.3.4.2  nathanw }
    210  1.3.4.2  nathanw 
    211  1.3.4.2  nathanw int
    212  1.3.4.2  nathanw opmsclose(dev, flag, mode, p)
    213  1.3.4.2  nathanw 	dev_t dev;
    214  1.3.4.2  nathanw 	int flag, mode;
    215  1.3.4.2  nathanw 	struct proc *p;
    216  1.3.4.2  nathanw {
    217  1.3.4.2  nathanw 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    218  1.3.4.2  nathanw 
    219  1.3.4.2  nathanw 	/* Disable interrupts. */
    220  1.3.4.2  nathanw 	pms_dev_cmd(PMS_DEV_DISABLE);
    221  1.3.4.2  nathanw 	pms_pit_cmd(PMS_INT_DISABLE);
    222  1.3.4.2  nathanw 	pms_aux_cmd(PMS_AUX_DISABLE);
    223  1.3.4.2  nathanw 
    224  1.3.4.2  nathanw 	sc->sc_state &= ~PMS_OPEN;
    225  1.3.4.2  nathanw 
    226  1.3.4.2  nathanw 	clfree(&sc->sc_q);
    227  1.3.4.2  nathanw 
    228  1.3.4.2  nathanw 	return 0;
    229  1.3.4.2  nathanw }
    230  1.3.4.2  nathanw 
    231  1.3.4.2  nathanw int
    232  1.3.4.2  nathanw opmsread(dev, uio, flag)
    233  1.3.4.2  nathanw 	dev_t dev;
    234  1.3.4.2  nathanw 	struct uio *uio;
    235  1.3.4.2  nathanw 	int flag;
    236  1.3.4.2  nathanw {
    237  1.3.4.2  nathanw 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    238  1.3.4.2  nathanw 	int s;
    239  1.3.4.2  nathanw 	int error = 0;
    240  1.3.4.2  nathanw 	size_t length;
    241  1.3.4.2  nathanw 	u_char buffer[PMS_CHUNK];
    242  1.3.4.2  nathanw 
    243  1.3.4.2  nathanw 	/* Block until mouse activity occurred. */
    244  1.3.4.2  nathanw 
    245  1.3.4.2  nathanw 	s = spltty();
    246  1.3.4.2  nathanw 	while (sc->sc_q.c_cc == 0) {
    247  1.3.4.2  nathanw 		if (flag & IO_NDELAY) {
    248  1.3.4.2  nathanw 			splx(s);
    249  1.3.4.2  nathanw 			return EWOULDBLOCK;
    250  1.3.4.2  nathanw 		}
    251  1.3.4.2  nathanw 		sc->sc_state |= PMS_ASLP;
    252  1.3.4.2  nathanw 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "pmsrea", 0);
    253  1.3.4.2  nathanw 		if (error) {
    254  1.3.4.2  nathanw 			sc->sc_state &= ~PMS_ASLP;
    255  1.3.4.2  nathanw 			splx(s);
    256  1.3.4.2  nathanw 			return error;
    257  1.3.4.2  nathanw 		}
    258  1.3.4.2  nathanw 	}
    259  1.3.4.2  nathanw 	splx(s);
    260  1.3.4.2  nathanw 
    261  1.3.4.2  nathanw 	/* Transfer as many chunks as possible. */
    262  1.3.4.2  nathanw 
    263  1.3.4.2  nathanw 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    264  1.3.4.2  nathanw 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    265  1.3.4.2  nathanw 		if (length > sizeof(buffer))
    266  1.3.4.2  nathanw 			length = sizeof(buffer);
    267  1.3.4.2  nathanw 
    268  1.3.4.2  nathanw 		/* Remove a small chunk from the input queue. */
    269  1.3.4.2  nathanw 		(void) q_to_b(&sc->sc_q, buffer, length);
    270  1.3.4.2  nathanw 
    271  1.3.4.2  nathanw 		/* Copy the data to the user process. */
    272  1.3.4.2  nathanw 		error = uiomove(buffer, length, uio);
    273  1.3.4.2  nathanw 		if (error)
    274  1.3.4.2  nathanw 			break;
    275  1.3.4.2  nathanw 	}
    276  1.3.4.2  nathanw 
    277  1.3.4.2  nathanw 	return error;
    278  1.3.4.2  nathanw }
    279  1.3.4.2  nathanw 
    280  1.3.4.2  nathanw int
    281  1.3.4.2  nathanw opmsioctl(dev, cmd, addr, flag, p)
    282  1.3.4.2  nathanw 	dev_t dev;
    283  1.3.4.2  nathanw 	u_long cmd;
    284  1.3.4.2  nathanw 	caddr_t addr;
    285  1.3.4.2  nathanw 	int flag;
    286  1.3.4.2  nathanw 	struct proc *p;
    287  1.3.4.2  nathanw {
    288  1.3.4.2  nathanw 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    289  1.3.4.2  nathanw 	struct mouseinfo info;
    290  1.3.4.2  nathanw 	int s;
    291  1.3.4.2  nathanw 	int error;
    292  1.3.4.2  nathanw 
    293  1.3.4.2  nathanw 	switch (cmd) {
    294  1.3.4.2  nathanw 	case MOUSEIOCREAD:
    295  1.3.4.2  nathanw 		s = spltty();
    296  1.3.4.2  nathanw 
    297  1.3.4.2  nathanw 		info.status = sc->sc_status;
    298  1.3.4.2  nathanw 		if (sc->sc_x || sc->sc_y)
    299  1.3.4.2  nathanw 			info.status |= MOVEMENT;
    300  1.3.4.2  nathanw 
    301  1.3.4.2  nathanw 		if (sc->sc_x > 127)
    302  1.3.4.2  nathanw 			info.xmotion = 127;
    303  1.3.4.2  nathanw 		else if (sc->sc_x < -127)
    304  1.3.4.2  nathanw 			/* Bounding at -127 avoids a bug in XFree86. */
    305  1.3.4.2  nathanw 			info.xmotion = -127;
    306  1.3.4.2  nathanw 		else
    307  1.3.4.2  nathanw 			info.xmotion = sc->sc_x;
    308  1.3.4.2  nathanw 
    309  1.3.4.2  nathanw 		if (sc->sc_y > 127)
    310  1.3.4.2  nathanw 			info.ymotion = 127;
    311  1.3.4.2  nathanw 		else if (sc->sc_y < -127)
    312  1.3.4.2  nathanw 			info.ymotion = -127;
    313  1.3.4.2  nathanw 		else
    314  1.3.4.2  nathanw 			info.ymotion = sc->sc_y;
    315  1.3.4.2  nathanw 
    316  1.3.4.2  nathanw 		/* Reset historical information. */
    317  1.3.4.2  nathanw 		sc->sc_x = sc->sc_y = 0;
    318  1.3.4.2  nathanw 		sc->sc_status &= ~BUTCHNGMASK;
    319  1.3.4.2  nathanw 		ndflush(&sc->sc_q, sc->sc_q.c_cc);
    320  1.3.4.2  nathanw 
    321  1.3.4.2  nathanw 		splx(s);
    322  1.3.4.2  nathanw 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    323  1.3.4.2  nathanw 		break;
    324  1.3.4.2  nathanw 	default:
    325  1.3.4.2  nathanw 		error = EINVAL;
    326  1.3.4.2  nathanw 		break;
    327  1.3.4.2  nathanw 	}
    328  1.3.4.2  nathanw 
    329  1.3.4.2  nathanw 	return error;
    330  1.3.4.2  nathanw }
    331  1.3.4.2  nathanw 
    332  1.3.4.2  nathanw /* Masks for the first byte of a packet */
    333  1.3.4.2  nathanw #define PS2LBUTMASK 0x01
    334  1.3.4.2  nathanw #define PS2RBUTMASK 0x02
    335  1.3.4.2  nathanw #define PS2MBUTMASK 0x04
    336  1.3.4.2  nathanw 
    337  1.3.4.2  nathanw int
    338  1.3.4.2  nathanw opmsintr(arg)
    339  1.3.4.2  nathanw 	void *arg;
    340  1.3.4.2  nathanw {
    341  1.3.4.2  nathanw 	struct opms_softc *sc = arg;
    342  1.3.4.2  nathanw 	static int state = 0;
    343  1.3.4.2  nathanw 	static u_char buttons;
    344  1.3.4.2  nathanw 	u_char changed;
    345  1.3.4.2  nathanw 	static char dx, dy;
    346  1.3.4.2  nathanw 	u_char buffer[5];
    347  1.3.4.2  nathanw 
    348  1.3.4.2  nathanw 	if ((sc->sc_state & PMS_OPEN) == 0) {
    349  1.3.4.2  nathanw 		/* Interrupts are not expected.  Discard the byte. */
    350  1.3.4.2  nathanw 		kbd_flush_input();
    351  1.3.4.2  nathanw 		return 0;
    352  1.3.4.2  nathanw 	}
    353  1.3.4.2  nathanw 
    354  1.3.4.2  nathanw 	switch (state) {
    355  1.3.4.2  nathanw 
    356  1.3.4.2  nathanw 	case 0:
    357  1.3.4.2  nathanw 		buttons = kbd_data_read_1();
    358  1.3.4.2  nathanw 		if ((buttons & 0xc0) == 0)
    359  1.3.4.2  nathanw 			++state;
    360  1.3.4.2  nathanw 		break;
    361  1.3.4.2  nathanw 
    362  1.3.4.2  nathanw 	case 1:
    363  1.3.4.2  nathanw 		dx = kbd_data_read_1();
    364  1.3.4.2  nathanw 		/* Bounding at -127 avoids a bug in XFree86. */
    365  1.3.4.2  nathanw 		dx = (dx == -128) ? -127 : dx;
    366  1.3.4.2  nathanw 		++state;
    367  1.3.4.2  nathanw 		break;
    368  1.3.4.2  nathanw 
    369  1.3.4.2  nathanw 	case 2:
    370  1.3.4.2  nathanw 		dy = kbd_data_read_1();
    371  1.3.4.2  nathanw 		dy = (dy == -128) ? -127 : dy;
    372  1.3.4.2  nathanw 		state = 0;
    373  1.3.4.2  nathanw 
    374  1.3.4.2  nathanw 		buttons = ((buttons & PS2LBUTMASK) << 2) |
    375  1.3.4.2  nathanw 			  ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
    376  1.3.4.2  nathanw 		changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
    377  1.3.4.2  nathanw 		sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    378  1.3.4.2  nathanw 
    379  1.3.4.2  nathanw 		if (dx || dy || changed) {
    380  1.3.4.2  nathanw 			/* Update accumulated movements. */
    381  1.3.4.2  nathanw 			sc->sc_x += dx;
    382  1.3.4.2  nathanw 			sc->sc_y += dy;
    383  1.3.4.2  nathanw 
    384  1.3.4.2  nathanw 			/* Add this event to the queue. */
    385  1.3.4.2  nathanw 			buffer[0] = 0x80 | (buttons & BUTSTATMASK);
    386  1.3.4.2  nathanw 			if(dx < 0)
    387  1.3.4.2  nathanw 				buffer[0] |= 0x10;
    388  1.3.4.2  nathanw 			buffer[1] = dx & 0x7f;
    389  1.3.4.2  nathanw 			if(dy < 0)
    390  1.3.4.2  nathanw 				buffer[0] |= 0x20;
    391  1.3.4.2  nathanw 			buffer[2] = dy & 0x7f;
    392  1.3.4.2  nathanw 			buffer[3] = buffer[4] = 0;
    393  1.3.4.2  nathanw 			(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    394  1.3.4.2  nathanw 
    395  1.3.4.2  nathanw 			if (sc->sc_state & PMS_ASLP) {
    396  1.3.4.2  nathanw 				sc->sc_state &= ~PMS_ASLP;
    397  1.3.4.2  nathanw 				wakeup((caddr_t)sc);
    398  1.3.4.2  nathanw 			}
    399  1.3.4.2  nathanw 			selwakeup(&sc->sc_rsel);
    400  1.3.4.2  nathanw 		}
    401  1.3.4.2  nathanw 
    402  1.3.4.2  nathanw 		break;
    403  1.3.4.2  nathanw 	}
    404  1.3.4.2  nathanw 	return -1;
    405  1.3.4.2  nathanw }
    406  1.3.4.2  nathanw 
    407  1.3.4.2  nathanw int
    408  1.3.4.2  nathanw opmspoll(dev, events, p)
    409  1.3.4.2  nathanw 	dev_t dev;
    410  1.3.4.2  nathanw 	int events;
    411  1.3.4.2  nathanw 	struct proc *p;
    412  1.3.4.2  nathanw {
    413  1.3.4.2  nathanw 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    414  1.3.4.2  nathanw 	int revents = 0;
    415  1.3.4.2  nathanw 	int s = spltty();
    416  1.3.4.2  nathanw 
    417  1.3.4.2  nathanw 	if (events & (POLLIN | POLLRDNORM)) {
    418  1.3.4.2  nathanw 		if (sc->sc_q.c_cc > 0)
    419  1.3.4.2  nathanw 			revents |= events & (POLLIN | POLLRDNORM);
    420  1.3.4.2  nathanw 		else
    421  1.3.4.2  nathanw 			selrecord(p, &sc->sc_rsel);
    422  1.3.4.2  nathanw 	}
    423  1.3.4.2  nathanw 
    424  1.3.4.2  nathanw 	splx(s);
    425  1.3.4.2  nathanw 	return (revents);
    426  1.3.4.2  nathanw }
    427