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