Home | History | Annotate | Line # | Download | only in isa
mms.c revision 1.8
      1  1.1   andrew /*-
      2  1.8  mycroft  * Copyright (c) 1993, 1994 Charles Hannum.
      3  1.1   andrew  * Copyright (c) 1992, 1993 Erik Forsberg.
      4  1.1   andrew  * All rights reserved.
      5  1.1   andrew  *
      6  1.1   andrew  * Redistribution and use in source and binary forms, with or without
      7  1.1   andrew  * modification, are permitted provided that the following conditions
      8  1.1   andrew  * are met:
      9  1.1   andrew  * 1. Redistributions of source code must retain the above copyright
     10  1.1   andrew  *    notice, this list of conditions and the following disclaimer.
     11  1.1   andrew  *
     12  1.1   andrew  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
     13  1.1   andrew  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     14  1.1   andrew  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
     15  1.1   andrew  * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     16  1.1   andrew  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     17  1.1   andrew  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     18  1.1   andrew  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     19  1.1   andrew  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     20  1.1   andrew  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     21  1.1   andrew  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     22  1.1   andrew  *
     23  1.8  mycroft  *	$Id: mms.c,v 1.8 1994/02/17 03:39:55 mycroft Exp $
     24  1.1   andrew  */
     25  1.1   andrew 
     26  1.2   andrew #include "mms.h"
     27  1.1   andrew 
     28  1.7  mycroft #include <sys/param.h>
     29  1.7  mycroft #include <sys/kernel.h>
     30  1.7  mycroft #include <sys/systm.h>
     31  1.7  mycroft #include <sys/buf.h>
     32  1.7  mycroft #include <sys/malloc.h>
     33  1.7  mycroft #include <sys/ioctl.h>
     34  1.7  mycroft #include <sys/tty.h>
     35  1.7  mycroft #include <sys/file.h>
     36  1.7  mycroft #include <sys/select.h>
     37  1.7  mycroft #include <sys/proc.h>
     38  1.7  mycroft #include <sys/vnode.h>
     39  1.8  mycroft #include <sys/device.h>
     40  1.7  mycroft 
     41  1.8  mycroft #include <machine/cpu.h>
     42  1.8  mycroft #include <machine/pio.h>
     43  1.7  mycroft #include <machine/mouse.h>
     44  1.1   andrew 
     45  1.7  mycroft #include <i386/isa/isa_device.h>
     46  1.1   andrew 
     47  1.8  mycroft #define	MMS_ADDR	0	/* offset for register select */
     48  1.8  mycroft #define	MMS_DATA	1	/* offset for InPort data */
     49  1.8  mycroft #define	MMS_IDENT	2	/* offset for identification register */
     50  1.8  mycroft #define	MMS_NPORTS	4
     51  1.8  mycroft 
     52  1.8  mycroft #define	MMS_CHUNK	128	/* chunk size for read */
     53  1.8  mycroft #define	MMS_BSIZE	1020	/* buffer size */
     54  1.8  mycroft 
     55  1.8  mycroft struct mms_softc {		/* driver status information */
     56  1.8  mycroft 	struct device sc_dev;
     57  1.8  mycroft 
     58  1.8  mycroft 	struct clist sc_q;
     59  1.8  mycroft 	struct selinfo sc_rsel;
     60  1.8  mycroft 	u_short sc_iobase;	/* I/O port base */
     61  1.8  mycroft 	u_char sc_state;	/* mouse driver state */
     62  1.8  mycroft #define	MMS_OPEN	0x01	/* device is open */
     63  1.8  mycroft #define	MMS_ASLP	0x02	/* waiting for mouse data */
     64  1.8  mycroft 	u_char sc_status;	/* mouse button status */
     65  1.8  mycroft 	int sc_x, sc_y;		/* accumulated motion in the X,Y axis */
     66  1.2   andrew } mms_softc[NMMS];
     67  1.1   andrew 
     68  1.8  mycroft int mmsprobe __P((struct isa_device *));
     69  1.8  mycroft int mmsattach __P((struct isa_device *));
     70  1.8  mycroft int mmsintr __P((int));
     71  1.1   andrew 
     72  1.2   andrew struct isa_driver mmsdriver = { mmsprobe, mmsattach, "mms" };
     73  1.1   andrew 
     74  1.8  mycroft #define	MMSUNIT(dev)	(minor(dev))
     75  1.8  mycroft 
     76  1.8  mycroft int
     77  1.8  mycroft mmsprobe(isa_dev)
     78  1.8  mycroft 	struct isa_device *isa_dev;
     79  1.1   andrew {
     80  1.8  mycroft 	u_short iobase = isa_dev->id_iobase;
     81  1.1   andrew 
     82  1.1   andrew 	/* Read identification register to see if present */
     83  1.8  mycroft 	if (inb(iobase + MMS_IDENT) != 0xde)
     84  1.8  mycroft 		return 0;
     85  1.1   andrew 
     86  1.8  mycroft 	/* Seems it was there; reset. */
     87  1.8  mycroft 	outb(iobase + MMS_ADDR, 0x87);
     88  1.1   andrew 
     89  1.8  mycroft 	return MMS_NPORTS;
     90  1.1   andrew }
     91  1.1   andrew 
     92  1.8  mycroft int
     93  1.8  mycroft mmsattach(isa_dev)
     94  1.8  mycroft 	struct isa_device *isa_dev;
     95  1.1   andrew {
     96  1.8  mycroft 	struct mms_softc *sc = &mms_softc[isa_dev->id_unit];
     97  1.8  mycroft 	u_short iobase = isa_dev->id_iobase;
     98  1.1   andrew 
     99  1.8  mycroft 	/* Other initialization was done by mmsprobe. */
    100  1.8  mycroft 	sc->sc_iobase = iobase;
    101  1.8  mycroft 	sc->sc_state = 0;
    102  1.8  mycroft 
    103  1.8  mycroft 	/* XXX HACK */
    104  1.8  mycroft 	sprintf(sc->sc_dev.dv_xname, "%s%d", mmsdriver.name, isa_dev->id_unit);
    105  1.8  mycroft 	sc->sc_dev.dv_unit = isa_dev->id_unit;
    106  1.1   andrew }
    107  1.1   andrew 
    108  1.8  mycroft int
    109  1.8  mycroft mmsopen(dev, flag)
    110  1.8  mycroft 	dev_t dev;
    111  1.8  mycroft 	int flag;
    112  1.1   andrew {
    113  1.2   andrew 	int unit = MMSUNIT(dev);
    114  1.2   andrew 	struct mms_softc *sc;
    115  1.1   andrew 
    116  1.2   andrew 	if (unit >= NMMS)
    117  1.8  mycroft 		return ENXIO;
    118  1.2   andrew 	sc = &mms_softc[unit];
    119  1.8  mycroft 	if (!sc->sc_iobase)
    120  1.8  mycroft 		return ENXIO;
    121  1.1   andrew 
    122  1.8  mycroft 	if (sc->sc_state & MMS_OPEN)
    123  1.8  mycroft 		return EBUSY;
    124  1.1   andrew 
    125  1.8  mycroft 	if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1)
    126  1.8  mycroft 		return ENOMEM;
    127  1.1   andrew 
    128  1.8  mycroft 	sc->sc_state |= MMS_OPEN;
    129  1.8  mycroft 	sc->sc_status = 0;
    130  1.8  mycroft 	sc->sc_x = sc->sc_y = 0;
    131  1.1   andrew 
    132  1.8  mycroft 	/* Enable interrupts. */
    133  1.8  mycroft 	outb(sc->sc_iobase + MMS_ADDR, 0x07);
    134  1.8  mycroft 	outb(sc->sc_iobase + MMS_DATA, 0x09);
    135  1.1   andrew 
    136  1.8  mycroft 	return 0;
    137  1.1   andrew }
    138  1.1   andrew 
    139  1.8  mycroft int
    140  1.8  mycroft mmsclose(dev, flag)
    141  1.8  mycroft 	dev_t dev;
    142  1.8  mycroft 	int flag;
    143  1.1   andrew {
    144  1.8  mycroft 	struct mms_softc *sc = &mms_softc[MMSUNIT(dev)];
    145  1.1   andrew 
    146  1.8  mycroft 	/* Disable interrupts. */
    147  1.8  mycroft 	outb(sc->sc_iobase + MMS_ADDR, 0x87);
    148  1.1   andrew 
    149  1.8  mycroft 	sc->sc_state &= ~MMS_OPEN;
    150  1.1   andrew 
    151  1.8  mycroft 	clfree(&sc->sc_q);
    152  1.1   andrew 
    153  1.8  mycroft 	return 0;
    154  1.1   andrew }
    155  1.1   andrew 
    156  1.8  mycroft int
    157  1.8  mycroft mmsread(dev, uio, flag)
    158  1.8  mycroft 	dev_t dev;
    159  1.8  mycroft 	struct uio *uio;
    160  1.8  mycroft 	int flag;
    161  1.1   andrew {
    162  1.8  mycroft 	struct mms_softc *sc = &mms_softc[MMSUNIT(dev)];
    163  1.8  mycroft 	int s;
    164  1.8  mycroft 	int error;
    165  1.8  mycroft 	size_t length;
    166  1.8  mycroft 	u_char buffer[MMS_CHUNK];
    167  1.1   andrew 
    168  1.8  mycroft 	/* Block until mouse activity occured. */
    169  1.1   andrew 
    170  1.1   andrew 	s = spltty();
    171  1.8  mycroft 	while (sc->sc_q.c_cc == 0) {
    172  1.8  mycroft 		if (flag & IO_NDELAY) {
    173  1.1   andrew 			splx(s);
    174  1.8  mycroft 			return EWOULDBLOCK;
    175  1.1   andrew 		}
    176  1.8  mycroft 		sc->sc_state |= MMS_ASLP;
    177  1.8  mycroft 		if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0)) {
    178  1.8  mycroft 			sc->sc_state &= ~MMS_ASLP;
    179  1.1   andrew 			splx(s);
    180  1.8  mycroft 			return error;
    181  1.1   andrew 		}
    182  1.1   andrew 	}
    183  1.8  mycroft 	splx(s);
    184  1.1   andrew 
    185  1.8  mycroft 	/* Transfer as many chunks as possible. */
    186  1.1   andrew 
    187  1.8  mycroft 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    188  1.8  mycroft 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    189  1.1   andrew 		if (length > sizeof(buffer))
    190  1.1   andrew 			length = sizeof(buffer);
    191  1.1   andrew 
    192  1.8  mycroft 		/* Remove a small chunk from the input queue. */
    193  1.8  mycroft 		(void) q_to_b(&sc->sc_q, buffer, length);
    194  1.1   andrew 
    195  1.8  mycroft 		/* Copy the data to the user process. */
    196  1.8  mycroft 		if (error = uiomove(buffer, length, uio))
    197  1.1   andrew 			break;
    198  1.1   andrew 	}
    199  1.1   andrew 
    200  1.8  mycroft 	return error;
    201  1.1   andrew }
    202  1.1   andrew 
    203  1.8  mycroft int
    204  1.8  mycroft mmsioctl(dev, cmd, addr, flag)
    205  1.8  mycroft 	dev_t dev;
    206  1.8  mycroft 	int cmd;
    207  1.8  mycroft 	caddr_t addr;
    208  1.8  mycroft 	int flag;
    209  1.1   andrew {
    210  1.8  mycroft 	struct mms_softc *sc = &mms_softc[MMSUNIT(dev)];
    211  1.1   andrew 	struct mouseinfo info;
    212  1.8  mycroft 	int s;
    213  1.8  mycroft 	int error;
    214  1.1   andrew 
    215  1.1   andrew 	switch (cmd) {
    216  1.1   andrew 	case MOUSEIOCREAD:
    217  1.1   andrew 		s = spltty();
    218  1.1   andrew 
    219  1.8  mycroft 		info.status = sc->sc_status;
    220  1.8  mycroft 		if (sc->sc_x || sc->sc_y)
    221  1.1   andrew 			info.status |= MOVEMENT;
    222  1.1   andrew 
    223  1.8  mycroft 		if (sc->sc_x > 127)
    224  1.1   andrew 			info.xmotion = 127;
    225  1.8  mycroft 		else if (sc->sc_x < -127)
    226  1.8  mycroft 			/* Bounding at -127 avoids a bug in XFree86. */
    227  1.8  mycroft 			info.xmotion = -127;
    228  1.1   andrew 		else
    229  1.8  mycroft 			info.xmotion = sc->sc_x;
    230  1.1   andrew 
    231  1.8  mycroft 		if (sc->sc_y > 127)
    232  1.1   andrew 			info.ymotion = 127;
    233  1.8  mycroft 		else if (sc->sc_y < -127)
    234  1.8  mycroft 			info.ymotion = -127;
    235  1.1   andrew 		else
    236  1.8  mycroft 			info.ymotion = sc->sc_y;
    237  1.1   andrew 
    238  1.8  mycroft 		/* Reset historical information. */
    239  1.8  mycroft 		sc->sc_x = sc->sc_y = 0;
    240  1.8  mycroft 		sc->sc_status &= ~BUTCHNGMASK;
    241  1.8  mycroft 		flushq(&sc->sc_q);
    242  1.1   andrew 
    243  1.1   andrew 		splx(s);
    244  1.1   andrew 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    245  1.1   andrew 		break;
    246  1.1   andrew 
    247  1.1   andrew 	default:
    248  1.1   andrew 		error = EINVAL;
    249  1.1   andrew 		break;
    250  1.8  mycroft 	}
    251  1.1   andrew 
    252  1.8  mycroft 	return error;
    253  1.1   andrew }
    254  1.1   andrew 
    255  1.8  mycroft int
    256  1.8  mycroft mmsintr(unit)
    257  1.1   andrew 	int unit;
    258  1.1   andrew {
    259  1.2   andrew 	struct mms_softc *sc = &mms_softc[unit];
    260  1.8  mycroft 	u_short iobase = sc->sc_iobase;
    261  1.8  mycroft 	u_char buttons, changed, status;
    262  1.8  mycroft 	char dx, dy;
    263  1.8  mycroft 	u_char buffer[5];
    264  1.8  mycroft 
    265  1.8  mycroft 	if ((sc->sc_state & MMS_OPEN) == 0)
    266  1.8  mycroft 		/* Interrupts are not expected. */
    267  1.8  mycroft 		return 0;
    268  1.8  mycroft 
    269  1.8  mycroft 	/* Freeze InPort registers (disabling interrupts). */
    270  1.8  mycroft 	outb(iobase + MMS_ADDR, 0x07);
    271  1.8  mycroft 	outb(iobase + MMS_DATA, 0x29);
    272  1.1   andrew 
    273  1.8  mycroft 	outb(iobase + MMS_ADDR, 0x00);
    274  1.8  mycroft 	status = inb(iobase + MMS_DATA);
    275  1.1   andrew 
    276  1.1   andrew 	if (status & 0x40) {
    277  1.8  mycroft 		outb(iobase + MMS_ADDR, 1);
    278  1.8  mycroft 		dx = inb(iobase + MMS_DATA);
    279  1.8  mycroft 		dx = (dx == -128) ? -127 : dx;
    280  1.8  mycroft 		outb(iobase + MMS_ADDR, 2);
    281  1.8  mycroft 		dy = inb(iobase + MMS_DATA);
    282  1.1   andrew 		dy = (dy == -128) ? 127 : -dy;
    283  1.8  mycroft 	} else
    284  1.1   andrew 		dx = dy = 0;
    285  1.1   andrew 
    286  1.8  mycroft 	/* Unfreeze InPort registers (reenabling interrupts). */
    287  1.8  mycroft 	outb(iobase + MMS_ADDR, 0x07);
    288  1.8  mycroft 	outb(iobase + MMS_DATA, 0x09);
    289  1.8  mycroft 
    290  1.8  mycroft 	buttons = status & BUTSTATMASK;
    291  1.8  mycroft 	changed = status & BUTCHNGMASK;
    292  1.8  mycroft 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    293  1.8  mycroft 
    294  1.8  mycroft 	if (dx || dy || changed) {
    295  1.8  mycroft 		/* Update accumulated movements. */
    296  1.8  mycroft 		sc->sc_x += dx;
    297  1.8  mycroft 		sc->sc_y += dy;
    298  1.8  mycroft 
    299  1.8  mycroft 		/* Add this event to the queue. */
    300  1.8  mycroft 		buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
    301  1.8  mycroft 		buffer[1] = dx;
    302  1.8  mycroft 		buffer[2] = dy;
    303  1.8  mycroft 		buffer[3] = buffer[4] = 0;
    304  1.8  mycroft 		(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    305  1.1   andrew 
    306  1.8  mycroft 		if (sc->sc_state & MMS_ASLP) {
    307  1.8  mycroft 			sc->sc_state &= ~MMS_ASLP;
    308  1.5   andrew 			wakeup((caddr_t)sc);
    309  1.1   andrew 		}
    310  1.8  mycroft 		selwakeup(&sc->sc_rsel);
    311  1.8  mycroft 	}
    312  1.8  mycroft 
    313  1.8  mycroft 	return 1;
    314  1.1   andrew }
    315  1.1   andrew 
    316  1.8  mycroft int
    317  1.8  mycroft mmsselect(dev, rw, p)
    318  1.8  mycroft 	dev_t dev;
    319  1.8  mycroft 	int rw;
    320  1.8  mycroft 	struct proc *p;
    321  1.1   andrew {
    322  1.2   andrew 	struct mms_softc *sc = &mms_softc[MMSUNIT(dev)];
    323  1.8  mycroft 	int s;
    324  1.8  mycroft 	int ret;
    325  1.1   andrew 
    326  1.1   andrew 	if (rw == FWRITE)
    327  1.8  mycroft 		return 0;
    328  1.1   andrew 
    329  1.1   andrew 	s = spltty();
    330  1.8  mycroft 	if (!sc->sc_q.c_cc) {
    331  1.8  mycroft 		selrecord(p, &sc->sc_rsel);
    332  1.8  mycroft 		ret = 0;
    333  1.8  mycroft 	} else
    334  1.1   andrew 		ret = 1;
    335  1.1   andrew 	splx(s);
    336  1.1   andrew 
    337  1.8  mycroft 	return ret;
    338  1.1   andrew }
    339