Home | History | Annotate | Line # | Download | only in isa
mms.c revision 1.6.2.3
      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.3 1993/09/30 20:19:08 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 
     39 #include "i386/isa/isavar.h"
     40 #include "i386/isa/isa.h"
     41 #include "i386/isa/icu.h"
     42 #include "i386/include/mouse.h"
     43 #include "i386/include/pio.h"
     44 
     45 #define MMS_ADDR	0	/* Offset for register select */
     46 #define MMS_DATA	1	/* Offset for InPort data */
     47 #define MMS_IDENT	2	/* Offset for identification register */
     48 #define	MMS_NPORTS	4
     49 
     50 #define	MMS_CHUNK	128	/* chunk size for read */
     51 #define	MMS_BSIZE	1024	/* buffer size */
     52 
     53 struct mms_softc {		/* Driver status information */
     54 	struct	device sc_dev;
     55 	struct	isadev sc_id;
     56 	struct	intrhand sc_ih;
     57 
     58 	struct	ringbuf {	/* Input queue */
     59 		int rb_count, rb_first, rb_last;
     60 		char rb_data[MMS_BSIZE];
     61 	} sc_q;
     62 	struct	selinfo sc_rsel;
     63 	u_short	sc_iobase;	/* I/O port base */
     64 	u_char	sc_flags;	/* Driver flags */
     65 #define	MMS_NOBLOCK	0x01
     66 	u_char	sc_state;	/* Mouse driver state */
     67 #define MMS_OPEN	0x01	/* Device is open */
     68 #define MMS_ASLP	0x02	/* Waiting for mouse data */
     69 	u_char	sc_status;	/* Mouse button status */
     70 	int	sc_x, sc_y;	/* accumulated motion in the X,Y axis */
     71 };
     72 
     73 static int mmsprobe __P((struct device *, struct cfdata *, void *));
     74 static void mmsforceintr __P((void *));
     75 static void mmsattach __P((struct device *, struct device *, void *));
     76 static int mmsintr __P((void *));
     77 
     78 struct cfdriver mmscd =
     79 { NULL, "mms", mmsprobe, mmsattach, sizeof (struct mms_softc) };
     80 
     81 #define MMSUNIT(dev)	(minor(dev) >> 1)
     82 #define	MMSFLAGS(dev)	(minor(dev) & 0x01)
     83 
     84 static int
     85 mmsprobe(parent, cf, aux)
     86 	struct device *parent;
     87 	struct cfdata *cf;
     88 	void *aux;
     89 {
     90 	struct	isa_attach_args *ia = aux;
     91 	u_short	iobase = ia->ia_iobase;
     92 
     93 	if (iobase == IOBASEUNK)
     94 		return 0;
     95 
     96 	/* Read identification register to see if present */
     97 	if (inb(iobase + MMS_IDENT) != 0xde)
     98 		return 0;
     99 
    100 	/* Seems it was there; reset */
    101 	outb(iobase + MMS_ADDR, 0x87);
    102 
    103 	if (ia->ia_irq == IRQUNK) {
    104 		ia->ia_irq = isa_discoverintr(mmsforceintr, aux);
    105 		if (ia->ia_irq == IRQNONE)
    106 			return 0;
    107 	}
    108 
    109 	/* reset again to disable interrupts */
    110 	outb(iobase + MMS_ADDR, 0x87);
    111 
    112 	ia->ia_iosize = MMS_NPORTS;
    113 	ia->ia_drq = DRQUNK;
    114 	ia->ia_msize = 0;
    115 	return 1;
    116 }
    117 
    118 static void
    119 mmsforceintr(aux)
    120 	void *aux;
    121 {
    122 	struct	isa_attach_args *ia = aux;
    123 	u_short	iobase = ia->ia_iobase;
    124 
    125 	/* enable interrupts; expect to get one in 1/60 second */
    126 	outb(iobase + MMS_ADDR, 0x07);
    127 	outb(iobase + MMS_DATA, 0x09);
    128 }
    129 
    130 static void
    131 mmsattach(parent, self, aux)
    132 	struct device *parent, *self;
    133 	void *aux;
    134 {
    135 	struct	mms_softc *sc = (struct mms_softc *)self;
    136 	struct	isa_attach_args *ia = aux;
    137 	u_short	iobase = ia->ia_iobase;
    138 
    139 	/* other initialization done by mmsprobe */
    140 	sc->sc_iobase = iobase;
    141 	sc->sc_state = 0;
    142 
    143 	printf(": Microsoft mouse\n");
    144 	isa_establish(&sc->sc_id, &sc->sc_dev);
    145 
    146 	sc->sc_ih.ih_fun = mmsintr;
    147 	sc->sc_ih.ih_arg = sc;
    148 	intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
    149 }
    150 
    151 int
    152 mmsopen(dev, flag)
    153 	dev_t dev;
    154 	int flag;
    155 {
    156 	int	unit = MMSUNIT(dev);
    157 	struct	mms_softc *sc;
    158 	u_short	iobase;
    159 
    160 	if (unit >= mmscd.cd_ndevs)
    161 		return ENXIO;
    162 	sc = mmscd.cd_devs[unit];
    163 	if (!sc)
    164 		return ENXIO;
    165 
    166 	if (sc->sc_state & MMS_OPEN)
    167 		return EBUSY;
    168 
    169 	sc->sc_state |= MMS_OPEN;
    170 	sc->sc_status = 0;
    171 	sc->sc_x = sc->sc_y = 0;
    172 	sc->sc_q.rb_count = sc->sc_q.rb_first = sc->sc_q.rb_last = 0;
    173 
    174 	/* enable interrupts */
    175 	iobase = sc->sc_iobase;
    176 	outb(iobase + MMS_ADDR, 0x07);
    177 	outb(iobase + MMS_DATA, 0x09);
    178 
    179 	return 0;
    180 }
    181 
    182 int
    183 mmsclose(dev, flag)
    184 	dev_t dev;
    185 	int flag;
    186 {
    187 	int	unit = MMSUNIT(dev);
    188 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    189 
    190 	/* disable interrupts */
    191 	outb(sc->sc_iobase + MMS_ADDR, 0x87);
    192 
    193 	sc->sc_state &= ~MMS_OPEN;
    194 
    195 	return 0;
    196 }
    197 
    198 int
    199 mmsread(dev, uio, flag)
    200 	dev_t dev;
    201 	struct uio *uio;
    202 	int flag;
    203 {
    204 	int	unit = MMSUNIT(dev);
    205 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    206 	int	s;
    207 	int	error;
    208 	size_t	length;
    209 	u_char	buffer[MMS_CHUNK];
    210 
    211 	/* Block until mouse activity occured */
    212 
    213 	s = spltty();
    214 	while (sc->sc_q.rb_count == 0) {
    215 		if (sc->sc_flags & MMS_NOBLOCK) {
    216 			splx(s);
    217 			return EWOULDBLOCK;
    218 		}
    219 		sc->sc_state |= MMS_ASLP;
    220 		if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0)) {
    221 			sc->sc_state &= ~MMS_ASLP;
    222 			splx(s);
    223 			return error;
    224 		}
    225 	}
    226 
    227 	/* Transfer as many chunks as possible */
    228 
    229 	while (sc->sc_q.rb_count > 0 && uio->uio_resid > 0) {
    230 		length = min(sc->sc_q.rb_count, uio->uio_resid);
    231 		if (length > sizeof(buffer))
    232 			length = sizeof(buffer);
    233 
    234 		/* Remove a small chunk from input queue */
    235 
    236 		if (sc->sc_q.rb_first + length >= MMS_BSIZE) {
    237 			size_t	left = MMS_BSIZE - sc->sc_q.rb_first;
    238 			bcopy(&sc->sc_q.rb_data[sc->sc_q.rb_first], buffer,
    239 			      left);
    240 			bcopy(sc->sc_q.rb_data, &buffer[left], length - left);
    241 		} else
    242 			bcopy(&sc->sc_q.rb_data[sc->sc_q.rb_first], buffer,
    243 			      length);
    244 
    245 		sc->sc_q.rb_first = (sc->sc_q.rb_first + length) % MMS_BSIZE;
    246 		sc->sc_q.rb_count -= length;
    247 
    248 		/* Copy data to user process */
    249 
    250 		if (error = uiomove(buffer, length, uio))
    251 			break;
    252 	}
    253 
    254 	/* reset counters */
    255 	sc->sc_x = sc->sc_y = 0;
    256 
    257 	splx(s);
    258 	return error;
    259 }
    260 
    261 int
    262 mmsioctl(dev, cmd, addr, flag)
    263 	dev_t dev;
    264 	int cmd;
    265 	caddr_t addr;
    266 	int flag;
    267 {
    268 	int	unit = MMSUNIT(dev);
    269 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    270 	struct	mouseinfo info;
    271 	int	s;
    272 	int	error;
    273 
    274 	switch (cmd) {
    275 	    case MOUSEIOCREAD:
    276 		s = spltty();
    277 
    278 		info.status = sc->sc_status;
    279 		if (sc->sc_x || sc->sc_y)
    280 			info.status |= MOVEMENT;
    281 
    282 		if (sc->sc_x > 127)
    283 			info.xmotion = 127;
    284 		else if (sc->sc_x < -127)
    285 			/* bounding at -127 avoids a bug in XFree86 */
    286 			info.xmotion = -127;
    287 		else
    288 			info.xmotion = sc->sc_x;
    289 
    290 		if (sc->sc_y > 127)
    291 			info.ymotion = 127;
    292 		else if (sc->sc_y < -127)
    293 			info.ymotion = -127;
    294 		else
    295 			info.ymotion = sc->sc_y;
    296 
    297 		sc->sc_x = 0;
    298 		sc->sc_y = 0;
    299 		sc->sc_status &= ~BUTCHNGMASK;
    300 
    301 		splx(s);
    302 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    303 		break;
    304 
    305 	    default:
    306 		error = EINVAL;
    307 		break;
    308 	}
    309 
    310 	return error;
    311 }
    312 
    313 int
    314 mmsintr(arg)
    315 	void *arg;
    316 {
    317 	struct	mms_softc *sc = (struct mms_softc *)arg;
    318 	u_short	iobase = sc->sc_iobase;
    319 	u_char	buttons, changed, status;
    320 	char	dx, dy;
    321 
    322 	if ((sc->sc_state & MMS_OPEN) == 0)
    323 		/* interrupts not expected */
    324 		return 0;
    325 
    326 	/* Freeze InPort registers (disabling interrupts) */
    327 	outb(iobase + MMS_ADDR, 0x07);
    328 	outb(iobase + MMS_DATA, 0x29);
    329 
    330 	outb(iobase + MMS_ADDR, 0);
    331 	status = inb(iobase + MMS_DATA);
    332 
    333 	if (status & 0x40) {
    334 		outb(iobase + MMS_ADDR, 1);
    335 		dx = inb(iobase + MMS_DATA);
    336 		dx = (dx == -128) ? -127 : dx;
    337 		outb(iobase + MMS_ADDR, 2);
    338 		dy = inb(iobase + MMS_DATA);
    339 		dy = (dy == -128) ? 127 : -dy;
    340 	} else
    341 		dx = dy = 0;
    342 
    343 	/* Unfreeze InPort Registers (re-enables interrupts) */
    344 	outb(iobase + MMS_ADDR, 0x07);
    345 	outb(iobase + MMS_DATA, 0x09);
    346 
    347 	buttons = status & BUTSTATMASK;
    348 	changed = status & BUTCHNGMASK;
    349 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    350 
    351 	if (dx || dy || changed) {
    352 		int	last = sc->sc_q.rb_last;
    353 		char	*cp = &sc->sc_q.rb_data[last];
    354 		int	count = sc->sc_q.rb_count;
    355 
    356 		/* Update accumulated movements */
    357 		sc->sc_x += dx;
    358 		sc->sc_y += dy;
    359 
    360 		if ((count += 5) > MMS_BSIZE)
    361 			return 1;
    362 		sc->sc_q.rb_count = count;
    363 
    364 #define	next() \
    365 		if (++last >= MMS_BSIZE) {	\
    366 			last = 0;		\
    367 			cp = sc->sc_q.rb_data;	\
    368 		} else				\
    369 			cp++;
    370 		*cp = 0x80 | (~status & BUTSTATMASK);
    371 		next();
    372 		*cp = dx;
    373 		next();
    374 		*cp = dy;
    375 		next();
    376 		*cp = 0;
    377 		next();
    378 		*cp = 0;
    379 		next();
    380 		sc->sc_q.rb_last = last;
    381 
    382 		if (sc->sc_state & MMS_ASLP) {
    383 			sc->sc_state &= ~MMS_ASLP;
    384 			wakeup((caddr_t)sc);
    385 		}
    386 		selwakeup(&sc->sc_rsel);
    387 	}
    388 
    389 	return 1;
    390 }
    391 
    392 int
    393 mmsselect(dev, rw, p)
    394 	dev_t dev;
    395 	int rw;
    396 	struct proc *p;
    397 {
    398 	int	unit = MMSUNIT(dev);
    399 	struct	mms_softc *sc = mmscd.cd_devs[unit];
    400 	int	s;
    401 	int	ret;
    402 
    403 	if (rw == FWRITE)
    404 		return 0;
    405 
    406 	s = spltty();
    407 	if (sc->sc_q.rb_count)
    408 		ret = 1;
    409 	else {
    410 		selrecord(p, &sc->sc_rsel);
    411 		ret = 0;
    412 	}
    413 	splx(s);
    414 
    415 	return ret;
    416 }
    417