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