Home | History | Annotate | Line # | Download | only in dev
opms.c revision 1.21.8.1
      1  1.21.8.1  pgoyette /*	$NetBSD: opms.c,v 1.21.8.1 2016/07/19 06:26:58 pgoyette Exp $	*/
      2       1.1      soda /*	$OpenBSD: pccons.c,v 1.22 1999/01/30 22:39:37 imp Exp $	*/
      3       1.1      soda /*	NetBSD: pms.c,v 1.21 1995/04/18 02:25:18 mycroft Exp	*/
      4       1.1      soda 
      5       1.1      soda /*-
      6       1.1      soda  * Copyright (c) 1990 The Regents of the University of California.
      7       1.1      soda  * All rights reserved.
      8       1.1      soda  *
      9       1.1      soda  * This code is derived from software contributed to Berkeley by
     10       1.1      soda  * William Jolitz and Don Ahn.
     11       1.1      soda  *
     12       1.1      soda  * Copyright (c) 1994 Charles M. Hannum.
     13       1.1      soda  * Copyright (c) 1992, 1993 Erik Forsberg.
     14       1.1      soda  *
     15       1.1      soda  * Redistribution and use in source and binary forms, with or without
     16       1.1      soda  * modification, are permitted provided that the following conditions
     17       1.1      soda  * are met:
     18       1.1      soda  * 1. Redistributions of source code must retain the above copyright
     19       1.1      soda  *    notice, this list of conditions and the following disclaimer.
     20       1.1      soda  * 2. Redistributions in binary form must reproduce the above copyright
     21       1.1      soda  *    notice, this list of conditions and the following disclaimer in the
     22       1.1      soda  *    documentation and/or other materials provided with the distribution.
     23      1.10       agc  * 3. Neither the name of the University nor the names of its contributors
     24      1.10       agc  *    may be used to endorse or promote products derived from this software
     25      1.10       agc  *    without specific prior written permission.
     26      1.10       agc  *
     27      1.10       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28      1.10       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29      1.10       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30      1.10       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31      1.10       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32      1.10       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33      1.10       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34      1.10       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35      1.10       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36      1.10       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37      1.10       agc  * SUCH DAMAGE.
     38      1.10       agc  *
     39      1.10       agc  *	@(#)pccons.c	5.11 (Berkeley) 5/21/91
     40      1.10       agc  */
     41      1.10       agc 
     42      1.10       agc /*-
     43      1.10       agc  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.  All rights reserved.
     44      1.10       agc  *
     45      1.10       agc  * This code is derived from software contributed to Berkeley by
     46      1.10       agc  * William Jolitz and Don Ahn.
     47      1.10       agc  *
     48      1.10       agc  * Copyright (c) 1994 Charles M. Hannum.
     49      1.10       agc  * Copyright (c) 1992, 1993 Erik Forsberg.
     50      1.10       agc  *
     51      1.10       agc  * Redistribution and use in source and binary forms, with or without
     52      1.10       agc  * modification, are permitted provided that the following conditions
     53      1.10       agc  * are met:
     54      1.10       agc  * 1. Redistributions of source code must retain the above copyright
     55      1.10       agc  *    notice, this list of conditions and the following disclaimer.
     56      1.10       agc  * 2. Redistributions in binary form must reproduce the above copyright
     57      1.10       agc  *    notice, this list of conditions and the following disclaimer in the
     58      1.10       agc  *    documentation and/or other materials provided with the distribution.
     59       1.1      soda  * 3. All advertising materials mentioning features or use of this software
     60       1.1      soda  *    must display the following acknowledgement:
     61       1.1      soda  *	This product includes software developed by the University of
     62       1.1      soda  *	California, Berkeley and its contributors.
     63       1.1      soda  * 4. Neither the name of the University nor the names of its contributors
     64       1.1      soda  *    may be used to endorse or promote products derived from this software
     65       1.1      soda  *    without specific prior written permission.
     66       1.1      soda  *
     67       1.1      soda  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     68       1.1      soda  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     69       1.1      soda  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     70       1.1      soda  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     71       1.1      soda  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     72       1.1      soda  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     73       1.1      soda  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     74       1.1      soda  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     75       1.1      soda  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     76       1.1      soda  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     77       1.1      soda  * SUCH DAMAGE.
     78       1.1      soda  *
     79       1.1      soda  *	@(#)pccons.c	5.11 (Berkeley) 5/21/91
     80       1.1      soda  */
     81       1.9     lukem 
     82       1.9     lukem #include <sys/cdefs.h>
     83  1.21.8.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: opms.c,v 1.21.8.1 2016/07/19 06:26:58 pgoyette Exp $");
     84       1.1      soda 
     85       1.1      soda #include <sys/param.h>
     86       1.1      soda #include <sys/systm.h>
     87       1.1      soda #include <sys/vnode.h>
     88       1.1      soda #include <sys/poll.h>
     89       1.1      soda #include <sys/tty.h>
     90       1.1      soda #include <sys/device.h>
     91       1.3       chs #include <sys/proc.h>
     92       1.4   gehenna #include <sys/conf.h>
     93       1.1      soda 
     94      1.19    dyoung #include <sys/bus.h>
     95       1.1      soda #include <machine/kbdreg.h>
     96       1.1      soda #include <machine/mouse.h>
     97       1.1      soda 
     98       1.1      soda #include <arc/dev/pcconsvar.h>
     99       1.1      soda #include <arc/dev/opmsvar.h>
    100       1.1      soda 
    101      1.15   tsutsui #include "ioconf.h"
    102      1.15   tsutsui 
    103       1.1      soda #define	PMSUNIT(dev)	(minor(dev))
    104       1.1      soda 
    105       1.1      soda /* status bits */
    106       1.1      soda #define	PMS_OBUF_FULL	0x01
    107       1.1      soda #define	PMS_IBUF_FULL	0x02
    108       1.1      soda 
    109       1.1      soda /* controller commands */
    110       1.1      soda #define	PMS_INT_ENABLE	0x47	/* enable controller interrupts */
    111       1.1      soda #define	PMS_INT_DISABLE	0x65	/* disable controller interrupts */
    112       1.1      soda #define	PMS_AUX_ENABLE	0xa7	/* enable auxiliary port */
    113       1.1      soda #define	PMS_AUX_DISABLE	0xa8	/* disable auxiliary port */
    114       1.1      soda #define	PMS_MAGIC_1	0xa9	/* XXX */
    115       1.1      soda 
    116       1.1      soda #define	PMS_8042_CMD	0x65
    117       1.1      soda 
    118       1.1      soda /* mouse commands */
    119       1.1      soda #define	PMS_SET_SCALE11	0xe6	/* set scaling 1:1 */
    120       1.1      soda #define	PMS_SET_SCALE21 0xe7	/* set scaling 2:1 */
    121       1.1      soda #define	PMS_SET_RES	0xe8	/* set resolution */
    122       1.1      soda #define	PMS_GET_SCALE	0xe9	/* get scaling factor */
    123       1.1      soda #define	PMS_SET_STREAM	0xea	/* set streaming mode */
    124       1.1      soda #define	PMS_SET_SAMPLE	0xf3	/* set sampling rate */
    125       1.1      soda #define	PMS_DEV_ENABLE	0xf4	/* mouse on */
    126       1.1      soda #define	PMS_DEV_DISABLE	0xf5	/* mouse off */
    127       1.1      soda #define	PMS_RESET	0xff	/* reset */
    128       1.1      soda 
    129       1.1      soda #define	PMS_CHUNK	128	/* chunk size for read */
    130       1.1      soda #define	PMS_BSIZE	1020	/* buffer size */
    131       1.1      soda 
    132       1.1      soda #define	FLUSHQ(q) { if((q)->c_cc) ndflush(q, (q)->c_cc); }
    133       1.1      soda 
    134       1.4   gehenna dev_type_open(opmsopen);
    135       1.4   gehenna dev_type_close(opmsclose);
    136       1.4   gehenna dev_type_read(opmsread);
    137       1.4   gehenna dev_type_ioctl(opmsioctl);
    138       1.4   gehenna dev_type_poll(opmspoll);
    139       1.6  jdolecek dev_type_kqfilter(opmskqfilter);
    140       1.4   gehenna 
    141       1.4   gehenna const struct cdevsw opms_cdevsw = {
    142      1.20  dholland 	.d_open = opmsopen,
    143      1.20  dholland 	.d_close = opmsclose,
    144      1.20  dholland 	.d_read = opmsread,
    145      1.20  dholland 	.d_write = nowrite,
    146      1.20  dholland 	.d_ioctl = opmsioctl,
    147      1.20  dholland 	.d_stop = nostop,
    148      1.20  dholland 	.d_tty = notty,
    149      1.20  dholland 	.d_poll = opmspoll,
    150      1.20  dholland 	.d_mmap = nommap,
    151      1.20  dholland 	.d_kqfilter = opmskqfilter,
    152      1.21  dholland 	.d_discard = nodiscard,
    153      1.20  dholland 	.d_flag = 0
    154       1.4   gehenna };
    155       1.4   gehenna 
    156      1.14     perry static inline void pms_dev_cmd(uint8_t);
    157      1.14     perry static inline void pms_aux_cmd(uint8_t);
    158      1.14     perry static inline void pms_pit_cmd(uint8_t);
    159       1.1      soda 
    160      1.14     perry static inline void
    161      1.11   tsutsui pms_dev_cmd(uint8_t value)
    162       1.1      soda {
    163      1.11   tsutsui 
    164       1.1      soda 	kbd_flush_input();
    165       1.1      soda 	kbd_cmd_write_1(0xd4);
    166       1.1      soda 	kbd_flush_input();
    167       1.1      soda 	kbd_data_write_1(value);
    168       1.1      soda }
    169       1.1      soda 
    170      1.14     perry static inline void
    171      1.11   tsutsui pms_aux_cmd(uint8_t value)
    172       1.1      soda {
    173      1.11   tsutsui 
    174       1.1      soda 	kbd_flush_input();
    175       1.1      soda 	kbd_cmd_write_1(value);
    176       1.1      soda }
    177       1.1      soda 
    178      1.14     perry static inline void
    179      1.11   tsutsui pms_pit_cmd(uint8_t value)
    180       1.1      soda {
    181      1.11   tsutsui 
    182       1.1      soda 	kbd_flush_input();
    183       1.1      soda 	kbd_cmd_write_1(0x60);
    184       1.1      soda 	kbd_flush_input();
    185       1.1      soda 	kbd_data_write_1(value);
    186       1.1      soda }
    187       1.1      soda 
    188      1.11   tsutsui int opms_common_match(bus_space_tag_t kbd_iot, struct pccons_config *config)
    189       1.1      soda {
    190      1.11   tsutsui 	uint8_t x;
    191       1.1      soda 
    192       1.1      soda 	kbd_context_init(kbd_iot, config);
    193       1.1      soda 
    194       1.1      soda 	pms_dev_cmd(KBC_RESET);
    195       1.1      soda 	pms_aux_cmd(PMS_MAGIC_1);
    196       1.1      soda 	delay(10000);
    197       1.1      soda 	x = kbd_data_read_1();
    198       1.1      soda 	pms_pit_cmd(PMS_INT_DISABLE);
    199       1.1      soda 	if (x & 0x04)
    200       1.1      soda 		return 0;
    201       1.1      soda 
    202       1.1      soda 	return 1;
    203       1.1      soda }
    204       1.1      soda 
    205       1.1      soda void
    206      1.11   tsutsui opms_common_attach(struct opms_softc *sc, bus_space_tag_t opms_iot,
    207      1.11   tsutsui     struct pccons_config *config)
    208       1.1      soda {
    209      1.11   tsutsui 
    210       1.1      soda 	kbd_context_init(opms_iot, config);
    211      1.17     rmind 	selinit(&sc->sc_rsel);
    212       1.1      soda 
    213       1.1      soda 	/* Other initialization was done by opmsprobe. */
    214       1.1      soda 	sc->sc_state = 0;
    215       1.1      soda }
    216       1.1      soda 
    217       1.1      soda int
    218      1.13  christos opmsopen(dev_t dev, int flag, int mode, struct lwp *l)
    219       1.1      soda {
    220  1.21.8.1  pgoyette 	device_t self;
    221       1.1      soda 	struct opms_softc *sc;
    222       1.1      soda 
    223  1.21.8.1  pgoyette 	self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
    224  1.21.8.1  pgoyette 	if (self == NULL)
    225  1.21.8.1  pgoyette 		return EXNIO;
    226  1.21.8.1  pgoyette 	sc = device_private(self);
    227  1.21.8.1  pgoyette 	if (!sc) {
    228  1.21.8.1  pgoyette 		device_release(self);
    229       1.1      soda 		return ENXIO;
    230  1.21.8.1  pgoyette 	}
    231       1.1      soda 
    232  1.21.8.1  pgoyette 	if (sc->sc_state & PMS_OPEN) {
    233  1.21.8.1  pgoyette 		device_release(self);
    234       1.1      soda 		return EBUSY;
    235  1.21.8.1  pgoyette 	}
    236       1.1      soda 
    237  1.21.8.1  pgoyette 	if (clalloc(&sc->sc_q, PMS_BSIZE, 0) == -1) {
    238  1.21.8.1  pgoyette 		device_release(self);
    239       1.1      soda 		return ENOMEM;
    240  1.21.8.1  pgoyette 	}
    241       1.1      soda 
    242       1.1      soda 	sc->sc_state |= PMS_OPEN;
    243       1.1      soda 	sc->sc_status = 0;
    244       1.1      soda 	sc->sc_x = sc->sc_y = 0;
    245       1.1      soda 
    246       1.1      soda 	/* Enable interrupts. */
    247       1.1      soda 	pms_dev_cmd(PMS_DEV_ENABLE);
    248       1.1      soda 	pms_aux_cmd(PMS_AUX_ENABLE);
    249       1.1      soda 	pms_dev_cmd(PMS_SET_RES);
    250       1.1      soda 	pms_dev_cmd(3);		/* 8 counts/mm */
    251       1.1      soda 	pms_dev_cmd(PMS_SET_SCALE21);
    252       1.1      soda #if 0
    253       1.1      soda 	pms_dev_cmd(PMS_SET_SAMPLE);
    254       1.1      soda 	pms_dev_cmd(100);	/* 100 samples/sec */
    255       1.1      soda 	pms_dev_cmd(PMS_SET_STREAM);
    256       1.1      soda #endif
    257       1.1      soda 	pms_pit_cmd(PMS_INT_ENABLE);
    258       1.1      soda 
    259  1.21.8.1  pgoyette 	device_release(self);
    260       1.1      soda 	return 0;
    261       1.1      soda }
    262       1.1      soda 
    263       1.1      soda int
    264      1.13  christos opmsclose(dev_t dev, int flag, int mode, struct lwp *l)
    265       1.1      soda {
    266  1.21.8.1  pgoyette 	device_t self;
    267  1.21.8.1  pgoyette 	struct opms_softc *sc;
    268  1.21.8.1  pgoyette 
    269  1.21.8.1  pgoyette 	self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
    270  1.21.8.1  pgoyette 	if (self == NULL)
    271  1.21.8.1  pgoyette 		return ENXIO;
    272  1.21.8.1  pgoyette 	sc = device_private(self);
    273  1.21.8.1  pgoyette 	if (sc == NULL) {
    274  1.21.8.1  pgoyette 		device_release(self);
    275  1.21.8.1  pgoyette 		return ENXIO;
    276  1.21.8.1  pgoyette 	}
    277       1.1      soda 
    278       1.1      soda 	/* Disable interrupts. */
    279       1.1      soda 	pms_dev_cmd(PMS_DEV_DISABLE);
    280       1.1      soda 	pms_pit_cmd(PMS_INT_DISABLE);
    281       1.1      soda 	pms_aux_cmd(PMS_AUX_DISABLE);
    282       1.1      soda 
    283       1.1      soda 	sc->sc_state &= ~PMS_OPEN;
    284       1.1      soda 
    285       1.1      soda 	clfree(&sc->sc_q);
    286       1.1      soda 
    287  1.21.8.1  pgoyette 	device_release(self);
    288       1.1      soda 	return 0;
    289       1.1      soda }
    290       1.1      soda 
    291       1.1      soda int
    292      1.12      matt opmsread(dev_t dev, struct uio *uio, int flag)
    293       1.1      soda {
    294  1.21.8.1  pgoyette 	device_t self;
    295  1.21.8.1  pgoyette 	struct opms_softc *sc;
    296       1.1      soda 	int s;
    297       1.1      soda 	int error = 0;
    298       1.1      soda 	size_t length;
    299       1.1      soda 	u_char buffer[PMS_CHUNK];
    300       1.1      soda 
    301  1.21.8.1  pgoyette 	self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
    302  1.21.8.1  pgoyette 	if (self == NULL)
    303  1.21.8.1  pgoyette 		return ENXIO;
    304  1.21.8.1  pgoyette 	sc = device_private(self);
    305  1.21.8.1  pgoyette 	if (sc == NULL) {
    306  1.21.8.1  pgoyette 		device_release(self);
    307  1.21.8.1  pgoyette 		return ENXIO;
    308  1.21.8.1  pgoyette 	}
    309  1.21.8.1  pgoyette 
    310       1.2       wiz 	/* Block until mouse activity occurred. */
    311       1.1      soda 
    312       1.1      soda 	s = spltty();
    313       1.1      soda 	while (sc->sc_q.c_cc == 0) {
    314       1.1      soda 		if (flag & IO_NDELAY) {
    315       1.1      soda 			splx(s);
    316  1.21.8.1  pgoyette 			device_release(self);
    317       1.1      soda 			return EWOULDBLOCK;
    318       1.1      soda 		}
    319       1.1      soda 		sc->sc_state |= PMS_ASLP;
    320      1.16  christos 		error = tsleep((void *)sc, PZERO | PCATCH, "pmsrea", 0);
    321       1.1      soda 		if (error) {
    322       1.1      soda 			sc->sc_state &= ~PMS_ASLP;
    323       1.1      soda 			splx(s);
    324  1.21.8.1  pgoyette 			device_release(self);
    325       1.1      soda 			return error;
    326       1.1      soda 		}
    327       1.1      soda 	}
    328       1.1      soda 	splx(s);
    329       1.1      soda 
    330       1.1      soda 	/* Transfer as many chunks as possible. */
    331       1.1      soda 
    332       1.1      soda 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    333       1.1      soda 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    334       1.1      soda 		if (length > sizeof(buffer))
    335       1.1      soda 			length = sizeof(buffer);
    336       1.1      soda 
    337       1.1      soda 		/* Remove a small chunk from the input queue. */
    338       1.1      soda 		(void) q_to_b(&sc->sc_q, buffer, length);
    339       1.1      soda 
    340       1.1      soda 		/* Copy the data to the user process. */
    341       1.1      soda 		error = uiomove(buffer, length, uio);
    342       1.1      soda 		if (error)
    343       1.1      soda 			break;
    344       1.1      soda 	}
    345       1.1      soda 
    346  1.21.8.1  pgoyette 	device_release(self);
    347       1.1      soda 	return error;
    348       1.1      soda }
    349       1.1      soda 
    350       1.1      soda int
    351      1.16  christos opmsioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    352       1.1      soda {
    353  1.21.8.1  pgoyette 	device_t self;
    354  1.21.8.1  pgoyette 	struct opms_softc *sc;
    355       1.1      soda 	struct mouseinfo info;
    356       1.1      soda 	int s;
    357       1.1      soda 	int error;
    358       1.1      soda 
    359  1.21.8.1  pgoyette 	self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
    360  1.21.8.1  pgoyette 	if (self == NULL)
    361  1.21.8.1  pgoyette 		return ENXIO;
    362  1.21.8.1  pgoyette 	sc = device_private(self);
    363  1.21.8.1  pgoyette 	if (sc == NULL) {
    364  1.21.8.1  pgoyette 		device_release(self);
    365  1.21.8.1  pgoyette 		return ENXIO;
    366  1.21.8.1  pgoyette 	}
    367       1.1      soda 	switch (cmd) {
    368       1.1      soda 	case MOUSEIOCREAD:
    369       1.1      soda 		s = spltty();
    370       1.1      soda 
    371       1.1      soda 		info.status = sc->sc_status;
    372       1.1      soda 		if (sc->sc_x || sc->sc_y)
    373       1.1      soda 			info.status |= MOVEMENT;
    374       1.1      soda 
    375       1.1      soda 		if (sc->sc_x > 127)
    376       1.1      soda 			info.xmotion = 127;
    377       1.1      soda 		else if (sc->sc_x < -127)
    378       1.1      soda 			/* Bounding at -127 avoids a bug in XFree86. */
    379       1.1      soda 			info.xmotion = -127;
    380       1.1      soda 		else
    381       1.1      soda 			info.xmotion = sc->sc_x;
    382       1.1      soda 
    383       1.1      soda 		if (sc->sc_y > 127)
    384       1.1      soda 			info.ymotion = 127;
    385       1.1      soda 		else if (sc->sc_y < -127)
    386       1.1      soda 			info.ymotion = -127;
    387       1.1      soda 		else
    388       1.1      soda 			info.ymotion = sc->sc_y;
    389       1.1      soda 
    390       1.1      soda 		/* Reset historical information. */
    391       1.1      soda 		sc->sc_x = sc->sc_y = 0;
    392       1.1      soda 		sc->sc_status &= ~BUTCHNGMASK;
    393       1.1      soda 		ndflush(&sc->sc_q, sc->sc_q.c_cc);
    394       1.1      soda 
    395       1.1      soda 		splx(s);
    396       1.1      soda 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    397       1.1      soda 		break;
    398       1.1      soda 	default:
    399       1.1      soda 		error = EINVAL;
    400       1.1      soda 		break;
    401       1.1      soda 	}
    402       1.1      soda 
    403  1.21.8.1  pgoyette 	device_release(self);
    404       1.1      soda 	return error;
    405       1.1      soda }
    406       1.1      soda 
    407       1.1      soda /* Masks for the first byte of a packet */
    408       1.1      soda #define PS2LBUTMASK 0x01
    409       1.1      soda #define PS2RBUTMASK 0x02
    410       1.1      soda #define PS2MBUTMASK 0x04
    411       1.1      soda 
    412       1.1      soda int
    413      1.11   tsutsui opmsintr(void *arg)
    414       1.1      soda {
    415       1.1      soda 	struct opms_softc *sc = arg;
    416       1.1      soda 	static int state = 0;
    417       1.1      soda 	static u_char buttons;
    418       1.1      soda 	u_char changed;
    419       1.1      soda 	static char dx, dy;
    420       1.1      soda 	u_char buffer[5];
    421       1.1      soda 
    422       1.1      soda 	if ((sc->sc_state & PMS_OPEN) == 0) {
    423       1.1      soda 		/* Interrupts are not expected.  Discard the byte. */
    424       1.1      soda 		kbd_flush_input();
    425       1.1      soda 		return 0;
    426       1.1      soda 	}
    427       1.1      soda 
    428       1.1      soda 	switch (state) {
    429       1.1      soda 
    430       1.1      soda 	case 0:
    431       1.1      soda 		buttons = kbd_data_read_1();
    432       1.1      soda 		if ((buttons & 0xc0) == 0)
    433       1.1      soda 			++state;
    434       1.1      soda 		break;
    435       1.1      soda 
    436       1.1      soda 	case 1:
    437       1.1      soda 		dx = kbd_data_read_1();
    438       1.1      soda 		/* Bounding at -127 avoids a bug in XFree86. */
    439       1.1      soda 		dx = (dx == -128) ? -127 : dx;
    440       1.1      soda 		++state;
    441       1.1      soda 		break;
    442       1.1      soda 
    443       1.1      soda 	case 2:
    444       1.1      soda 		dy = kbd_data_read_1();
    445       1.1      soda 		dy = (dy == -128) ? -127 : dy;
    446       1.1      soda 		state = 0;
    447       1.1      soda 
    448       1.1      soda 		buttons = ((buttons & PS2LBUTMASK) << 2) |
    449      1.11   tsutsui 		    ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
    450       1.1      soda 		changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
    451      1.11   tsutsui 		sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) |
    452      1.11   tsutsui 		    changed;
    453       1.1      soda 
    454       1.1      soda 		if (dx || dy || changed) {
    455       1.1      soda 			/* Update accumulated movements. */
    456       1.1      soda 			sc->sc_x += dx;
    457       1.1      soda 			sc->sc_y += dy;
    458       1.1      soda 
    459       1.1      soda 			/* Add this event to the queue. */
    460       1.1      soda 			buffer[0] = 0x80 | (buttons & BUTSTATMASK);
    461       1.1      soda 			if(dx < 0)
    462       1.1      soda 				buffer[0] |= 0x10;
    463       1.1      soda 			buffer[1] = dx & 0x7f;
    464       1.1      soda 			if(dy < 0)
    465       1.1      soda 				buffer[0] |= 0x20;
    466       1.1      soda 			buffer[2] = dy & 0x7f;
    467       1.1      soda 			buffer[3] = buffer[4] = 0;
    468       1.1      soda 			(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    469       1.1      soda 
    470       1.1      soda 			if (sc->sc_state & PMS_ASLP) {
    471       1.1      soda 				sc->sc_state &= ~PMS_ASLP;
    472      1.16  christos 				wakeup((void *)sc);
    473       1.1      soda 			}
    474      1.17     rmind 			selnotify(&sc->sc_rsel, 0, 0);
    475       1.1      soda 		}
    476       1.1      soda 
    477       1.1      soda 		break;
    478       1.1      soda 	}
    479       1.1      soda 	return -1;
    480       1.1      soda }
    481       1.1      soda 
    482       1.1      soda int
    483      1.13  christos opmspoll(dev_t dev, int events, struct lwp *l)
    484       1.1      soda {
    485  1.21.8.1  pgoyette 	device_t self;
    486  1.21.8.1  pgoyette 	struct opms_softc *sc;
    487       1.1      soda 	int revents = 0;
    488       1.1      soda 	int s = spltty();
    489       1.1      soda 
    490  1.21.8.1  pgoyette 	self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
    491  1.21.8.1  pgoyette 	if (self == NULL)
    492  1.21.8.1  pgoyette 		return 0;
    493  1.21.8.1  pgoyette 	sc = device_private(self);
    494  1.21.8.1  pgoyette 	if (sc == NULL) {
    495  1.21.8.1  pgoyette 		device_release(self);
    496  1.21.8.1  pgoyette 		return 0;
    497  1.21.8.1  pgoyette 	}
    498  1.21.8.1  pgoyette 
    499       1.1      soda 	if (events & (POLLIN | POLLRDNORM)) {
    500       1.1      soda 		if (sc->sc_q.c_cc > 0)
    501       1.1      soda 			revents |= events & (POLLIN | POLLRDNORM);
    502       1.1      soda 		else
    503      1.13  christos 			selrecord(l, &sc->sc_rsel);
    504       1.1      soda 	}
    505       1.1      soda 
    506       1.1      soda 	splx(s);
    507  1.21.8.1  pgoyette 	device_release(self);
    508      1.11   tsutsui 	return revents;
    509       1.5  jdolecek }
    510       1.5  jdolecek 
    511       1.5  jdolecek static void
    512       1.5  jdolecek filt_opmsrdetach(struct knote *kn)
    513       1.5  jdolecek {
    514       1.5  jdolecek 	struct opms_softc *sc = kn->kn_hook;
    515       1.8   tsutsui 	int s;
    516       1.5  jdolecek 
    517       1.5  jdolecek 	s = spltty();
    518       1.7  christos 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    519       1.5  jdolecek 	splx(s);
    520       1.5  jdolecek }
    521       1.5  jdolecek 
    522       1.5  jdolecek static int
    523       1.5  jdolecek filt_opmsread(struct knote *kn, long hint)
    524       1.5  jdolecek {
    525       1.5  jdolecek 	struct opms_softc *sc = kn->kn_hook;
    526       1.5  jdolecek 
    527       1.5  jdolecek 	kn->kn_data = sc->sc_q.c_cc;
    528      1.11   tsutsui 	return kn->kn_data > 0;
    529       1.5  jdolecek }
    530       1.5  jdolecek 
    531       1.5  jdolecek static const struct filterops opmsread_filtops =
    532       1.5  jdolecek 	{ 1, NULL, filt_opmsrdetach, filt_opmsread };
    533       1.5  jdolecek 
    534       1.5  jdolecek int
    535       1.5  jdolecek opmskqfilter(dev_t dev, struct knote *kn)
    536       1.5  jdolecek {
    537  1.21.8.1  pgoyette 	device_t self;
    538  1.21.8.1  pgoyette 	struct opms_softc *sc;
    539       1.5  jdolecek 	struct klist *klist;
    540       1.5  jdolecek 	int s;
    541       1.5  jdolecek 
    542  1.21.8.1  pgoyette 	self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
    543  1.21.8.1  pgoyette 	if (self == NULL)
    544  1.21.8.1  pgoyette 		return 1;
    545  1.21.8.1  pgoyette 	sc = device_private(self);
    546  1.21.8.1  pgoyette 	if (sc == NULL) {
    547  1.21.8.1  pgoyette 		device_release(self);
    548  1.21.8.1  pgoyette 		return 1;
    549  1.21.8.1  pgoyette 	}
    550  1.21.8.1  pgoyette 
    551       1.5  jdolecek 	switch (kn->kn_filter) {
    552       1.5  jdolecek 	case EVFILT_READ:
    553       1.7  christos 		klist = &sc->sc_rsel.sel_klist;
    554       1.5  jdolecek 		kn->kn_fop = &opmsread_filtops;
    555       1.5  jdolecek 		break;
    556       1.5  jdolecek 
    557       1.5  jdolecek 	default:
    558  1.21.8.1  pgoyette 		device_release(self);
    559      1.11   tsutsui 		return 1;
    560       1.5  jdolecek 	}
    561       1.5  jdolecek 
    562       1.5  jdolecek 	kn->kn_hook = sc;
    563       1.5  jdolecek 
    564       1.5  jdolecek 	s = spltty();
    565       1.5  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    566       1.5  jdolecek 	splx(s);
    567       1.5  jdolecek 
    568  1.21.8.1  pgoyette 	device_release(self);
    569      1.11   tsutsui 	return 0;
    570       1.1      soda }
    571