Home | History | Annotate | Line # | Download | only in isa
mms.c revision 1.6.2.5
      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.5 1993/10/06 12:10:31 mycroft Exp $
     24  */
     25 
     26 #include "param.h"
     27 #include "kernel.h"
     28 #include "systm.h"
     29 #include "buf.h"
     30 #include "malloc.h"
     31 #include "ioctl.h"
     32 #include "tty.h"
     33 #include "file.h"
     34 #include "select.h"
     35 #include "proc.h"
     36 #include "vnode.h"
     37 #include "sys/device.h"
     38 #include "machine/cpu.h"
     39 
     40 #include "i386/isa/isavar.h"
     41 #include "i386/isa/isa.h"
     42 #include "i386/isa/icu.h"
     43 #include "i386/include/mouse.h"
     44 #include "i386/include/pio.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, 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 	}
    103 
    104 	/* reset again to disable interrupts */
    105 	outb(iobase + MMS_ADDR, 0x87);
    106 
    107 	ia->ia_iosize = MMS_NPORTS;
    108 	ia->ia_drq = DRQUNK;
    109 	ia->ia_msize = 0;
    110 	return 1;
    111 }
    112 
    113 static void
    114 mmsforceintr(aux)
    115 	void *aux;
    116 {
    117 	struct	isa_attach_args *ia = aux;
    118 	u_short	iobase = ia->ia_iobase;
    119 
    120 	/* enable interrupts; expect to get one in 1/60 second */
    121 	outb(iobase + MMS_ADDR, 0x07);
    122 	outb(iobase + MMS_DATA, 0x09);
    123 }
    124 
    125 static void
    126 mmsattach(parent, self, aux)
    127 	struct device *parent, *self;
    128 	void *aux;
    129 {
    130 	struct	mms_softc *sc = (struct mms_softc *)self;
    131 	struct	isa_attach_args *ia = aux;
    132 	u_short	iobase = ia->ia_iobase;
    133 
    134 	/* other initialization done by mmsprobe */
    135 	sc->sc_iobase = iobase;
    136 	sc->sc_state = 0;
    137 
    138 	printf(": Microsoft mouse\n");
    139 	isa_establish(&sc->sc_id, &sc->sc_dev);
    140 
    141 	sc->sc_ih.ih_fun = mmsintr;
    142 	sc->sc_ih.ih_arg = sc;
    143 	intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
    144 }
    145 
    146 int
    147 mmsopen(dev, flag)
    148 	dev_t dev;
    149 	int flag;
    150 {
    151 	int	unit = MMSUNIT(dev);
    152 	struct	mms_softc *sc;
    153 	u_short	iobase;
    154 
    155 	if (unit >= mmscd.cd_ndevs)
    156 		return ENXIO;
    157 	sc = mmscd.cd_devs[unit];
    158 	if (!sc)
    159 		return ENXIO;
    160 
    161 	if (sc->sc_state & MMS_OPEN)
    162 		return EBUSY;
    163 
    164 	if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1)
    165 		return ENOMEM;
    166 
    167 	sc->sc_state |= MMS_OPEN;
    168 	sc->sc_status = 0;
    169 	sc->sc_x = sc->sc_y = 0;
    170 
    171 	/* enable interrupts */
    172 	iobase = sc->sc_iobase;
    173 	outb(iobase + MMS_ADDR, 0x07);
    174 	outb(iobase + MMS_DATA, 0x09);
    175 
    176 	return 0;
    177 }
    178 
    179 int
    180 mmsclose(dev, flag)
    181 	dev_t dev;
    182 	int flag;
    183 {
    184 	int	unit = MMSUNIT(dev);
    185 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    186 
    187 	/* disable interrupts */
    188 	outb(sc->sc_iobase + MMS_ADDR, 0x87);
    189 
    190 	sc->sc_state &= ~MMS_OPEN;
    191 
    192 	clfree(&sc->sc_q);
    193 
    194 	return 0;
    195 }
    196 
    197 int
    198 mmsread(dev, uio, flag)
    199 	dev_t dev;
    200 	struct uio *uio;
    201 	int flag;
    202 {
    203 	int	unit = MMSUNIT(dev);
    204 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    205 	int	s;
    206 	int	error;
    207 	size_t	length;
    208 	u_char	buffer[MMS_CHUNK];
    209 
    210 	/* Block until mouse activity occured */
    211 
    212 	s = spltty();
    213 	while (sc->sc_q.c_cc == 0) {
    214 		if (flag & IO_NDELAY) {
    215 			splx(s);
    216 			return EWOULDBLOCK;
    217 		}
    218 		sc->sc_state |= MMS_ASLP;
    219 		if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0)) {
    220 			sc->sc_state &= ~MMS_ASLP;
    221 			splx(s);
    222 			return error;
    223 		}
    224 	}
    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 	/* reset counters */
    242 	sc->sc_x = sc->sc_y = 0;
    243 
    244 	splx(s);
    245 	return error;
    246 }
    247 
    248 int
    249 mmsioctl(dev, cmd, addr, flag)
    250 	dev_t dev;
    251 	int cmd;
    252 	caddr_t addr;
    253 	int flag;
    254 {
    255 	int	unit = MMSUNIT(dev);
    256 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    257 	struct	mouseinfo info;
    258 	int	s;
    259 	int	error;
    260 
    261 	switch (cmd) {
    262 	    case MOUSEIOCREAD:
    263 		s = spltty();
    264 
    265 		info.status = sc->sc_status;
    266 		if (sc->sc_x || sc->sc_y)
    267 			info.status |= MOVEMENT;
    268 
    269 		if (sc->sc_x > 127)
    270 			info.xmotion = 127;
    271 		else if (sc->sc_x < -127)
    272 			/* bounding at -127 avoids a bug in XFree86 */
    273 			info.xmotion = -127;
    274 		else
    275 			info.xmotion = sc->sc_x;
    276 
    277 		if (sc->sc_y > 127)
    278 			info.ymotion = 127;
    279 		else if (sc->sc_y < -127)
    280 			info.ymotion = -127;
    281 		else
    282 			info.ymotion = sc->sc_y;
    283 
    284 		/* reset historical information */
    285 		sc->sc_x = 0;
    286 		sc->sc_y = 0;
    287 		sc->sc_status &= ~BUTCHNGMASK;
    288 		flushq(&sc->sc_q);
    289 
    290 		splx(s);
    291 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    292 		break;
    293 
    294 	    default:
    295 		error = EINVAL;
    296 		break;
    297 	}
    298 
    299 	return error;
    300 }
    301 
    302 int
    303 mmsintr(arg)
    304 	void *arg;
    305 {
    306 	struct	mms_softc *sc = (struct mms_softc *)arg;
    307 	u_short	iobase = sc->sc_iobase;
    308 	u_char	buttons, changed, status;
    309 	char	dx, dy;
    310 	u_char	buffer[5];
    311 
    312 	if ((sc->sc_state & MMS_OPEN) == 0)
    313 		/* interrupts not expected */
    314 		return 0;
    315 
    316 	/* Freeze InPort registers (disabling interrupts) */
    317 	outb(iobase + MMS_ADDR, 0x07);
    318 	outb(iobase + MMS_DATA, 0x29);
    319 
    320 	outb(iobase + MMS_ADDR, 0);
    321 	status = inb(iobase + MMS_DATA);
    322 
    323 	if (status & 0x40) {
    324 		outb(iobase + MMS_ADDR, 1);
    325 		dx = inb(iobase + MMS_DATA);
    326 		dx = (dx == -128) ? -127 : dx;
    327 		outb(iobase + MMS_ADDR, 2);
    328 		dy = inb(iobase + MMS_DATA);
    329 		dy = (dy == -128) ? 127 : -dy;
    330 	} else
    331 		dx = dy = 0;
    332 
    333 	/* Unfreeze InPort Registers (re-enables interrupts) */
    334 	outb(iobase + MMS_ADDR, 0x07);
    335 	outb(iobase + MMS_DATA, 0x09);
    336 
    337 	buttons = status & BUTSTATMASK;
    338 	changed = status & BUTCHNGMASK;
    339 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    340 
    341 	if (dx || dy || changed) {
    342 		/* update accumulated movements */
    343 		sc->sc_x += dx;
    344 		sc->sc_y += dy;
    345 
    346 		buffer[0] = 0x80 | (~status & BUTSTATMASK);
    347 		buffer[1] = dx;
    348 		buffer[2] = dy;
    349 		buffer[3] = buffer[4] = 0;
    350 		(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    351 
    352 		if (sc->sc_state & MMS_ASLP) {
    353 			sc->sc_state &= ~MMS_ASLP;
    354 			wakeup((caddr_t)sc);
    355 		}
    356 		selwakeup(&sc->sc_rsel);
    357 	}
    358 
    359 	return 1;
    360 }
    361 
    362 int
    363 mmsselect(dev, rw, p)
    364 	dev_t dev;
    365 	int rw;
    366 	struct proc *p;
    367 {
    368 	int	unit = MMSUNIT(dev);
    369 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    370 	int	s;
    371 	int	ret;
    372 
    373 	if (rw == FWRITE)
    374 		return 0;
    375 
    376 	s = spltty();
    377 	if (sc->sc_q.c_cc)
    378 		ret = 1;
    379 	else {
    380 		selrecord(p, &sc->sc_rsel);
    381 		ret = 0;
    382 	}
    383 	splx(s);
    384 
    385 	return ret;
    386 }
    387