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