Home | History | Annotate | Line # | Download | only in isa
mms.c revision 1.6.2.10
      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.10 1993/10/17 05:33:36 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 	}
    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 	splx(s);
    226 
    227 	/* Transfer as many chunks as possible */
    228 
    229 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    230 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    231 		if (length > sizeof(buffer))
    232 			length = sizeof(buffer);
    233 
    234 		/* remove a small chunk from input queue */
    235 		(void) q_to_b(&sc->sc_q, buffer, length);
    236 
    237 		/* copy data to user process */
    238 		if (error = uiomove(buffer, length, uio))
    239 			break;
    240 	}
    241 
    242 	return error;
    243 }
    244 
    245 int
    246 mmsioctl(dev, cmd, addr, flag)
    247 	dev_t dev;
    248 	int cmd;
    249 	caddr_t addr;
    250 	int flag;
    251 {
    252 	int	unit = MMSUNIT(dev);
    253 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    254 	struct	mouseinfo info;
    255 	int	s;
    256 	int	error;
    257 
    258 	switch (cmd) {
    259 	    case MOUSEIOCREAD:
    260 		s = spltty();
    261 
    262 		info.status = sc->sc_status;
    263 		if (sc->sc_x || sc->sc_y)
    264 			info.status |= MOVEMENT;
    265 
    266 		if (sc->sc_x > 127)
    267 			info.xmotion = 127;
    268 		else if (sc->sc_x < -127)
    269 			/* bounding at -127 avoids a bug in XFree86 */
    270 			info.xmotion = -127;
    271 		else
    272 			info.xmotion = sc->sc_x;
    273 
    274 		if (sc->sc_y > 127)
    275 			info.ymotion = 127;
    276 		else if (sc->sc_y < -127)
    277 			info.ymotion = -127;
    278 		else
    279 			info.ymotion = sc->sc_y;
    280 
    281 		/* reset historical information */
    282 		sc->sc_x = 0;
    283 		sc->sc_y = 0;
    284 		sc->sc_status &= ~BUTCHNGMASK;
    285 		flushq(&sc->sc_q);
    286 
    287 		splx(s);
    288 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    289 		break;
    290 
    291 	    default:
    292 		error = EINVAL;
    293 		break;
    294 	}
    295 
    296 	return error;
    297 }
    298 
    299 int
    300 mmsintr(arg)
    301 	void *arg;
    302 {
    303 	struct	mms_softc *sc = (struct mms_softc *)arg;
    304 	u_short	iobase = sc->sc_iobase;
    305 	u_char	buttons, changed, status;
    306 	char	dx, dy;
    307 	u_char	buffer[5];
    308 
    309 	if ((sc->sc_state & MMS_OPEN) == 0)
    310 		/* interrupts not expected */
    311 		return 0;
    312 
    313 	/* Freeze InPort registers (disabling interrupts) */
    314 	outb(iobase + MMS_ADDR, 0x07);
    315 	outb(iobase + MMS_DATA, 0x29);
    316 
    317 	outb(iobase + MMS_ADDR, 0);
    318 	status = inb(iobase + MMS_DATA);
    319 
    320 	if (status & 0x40) {
    321 		outb(iobase + MMS_ADDR, 1);
    322 		dx = inb(iobase + MMS_DATA);
    323 		dx = (dx == -128) ? -127 : dx;
    324 		outb(iobase + MMS_ADDR, 2);
    325 		dy = inb(iobase + MMS_DATA);
    326 		dy = (dy == -128) ? 127 : -dy;
    327 	} else
    328 		dx = dy = 0;
    329 
    330 	/* Unfreeze InPort Registers (re-enables interrupts) */
    331 	outb(iobase + MMS_ADDR, 0x07);
    332 	outb(iobase + MMS_DATA, 0x09);
    333 
    334 	buttons = status & BUTSTATMASK;
    335 	changed = status & BUTCHNGMASK;
    336 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    337 
    338 	if (dx || dy || changed) {
    339 		/* update accumulated movements */
    340 		sc->sc_x += dx;
    341 		sc->sc_y += dy;
    342 
    343 		buffer[0] = 0x80 | (~status & BUTSTATMASK);
    344 		buffer[1] = dx;
    345 		buffer[2] = dy;
    346 		buffer[3] = buffer[4] = 0;
    347 		(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    348 
    349 		if (sc->sc_state & MMS_ASLP) {
    350 			sc->sc_state &= ~MMS_ASLP;
    351 			wakeup((caddr_t)sc);
    352 		}
    353 		selwakeup(&sc->sc_rsel);
    354 	}
    355 
    356 	return 1;
    357 }
    358 
    359 int
    360 mmsselect(dev, rw, p)
    361 	dev_t dev;
    362 	int rw;
    363 	struct proc *p;
    364 {
    365 	int	unit = MMSUNIT(dev);
    366 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    367 	int	s;
    368 	int	ret;
    369 
    370 	if (rw == FWRITE)
    371 		return 0;
    372 
    373 	s = spltty();
    374 	if (sc->sc_q.c_cc)
    375 		ret = 1;
    376 	else {
    377 		selrecord(p, &sc->sc_rsel);
    378 		ret = 0;
    379 	}
    380 	splx(s);
    381 
    382 	return ret;
    383 }
    384