Home | History | Annotate | Line # | Download | only in isa
lms.c revision 1.10
      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: lms.c,v 1.10 1994/03/29 04:36:06 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/pio.h>
     41 #include <machine/mouse.h>
     42 
     43 #include <i386/isa/isavar.h>
     44 
     45 #define	LMS_DATA	0       /* offset for data port, read-only */
     46 #define	LMS_SIGN	1       /* offset for signature port, read-write */
     47 #define	LMS_INTR	2       /* offset for interrupt port, read-only */
     48 #define	LMS_CNTRL	2       /* offset for control port, write-only */
     49 #define	LMS_CONFIG	3	/* for configuration port, read-write */
     50 #define	LMS_NPORTS	4
     51 
     52 #define	LMS_CHUNK	128	/* chunk size for read */
     53 #define	LMS_BSIZE	1020	/* buffer size */
     54 
     55 struct lms_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	LMS_OPEN	0x01	/* device is open */
     63 #define	LMS_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 };
     67 
     68 int lmsprobe();
     69 void lmsattach();
     70 int lmsintr __P((int));
     71 
     72 struct cfdriver lmscd = {
     73 	NULL, "lms", lmsprobe, lmsattach, DV_TTY, sizeof(struct lms_softc)
     74 };
     75 
     76 #define	LMSUNIT(dev)	(minor(dev))
     77 
     78 int
     79 lmsprobe(parent, self, aux)
     80 	struct device *parent, *self;
     81 	void *aux;
     82 {
     83 	struct isa_attach_args *ia = aux;
     84 	u_short iobase = ia->ia_iobase;
     85 
     86 	/* Configure and check for port present. */
     87 	outb(iobase + LMS_CONFIG, 0x91);
     88 	delay(10);
     89 	outb(iobase + LMS_SIGN, 0x0c);
     90 	delay(10);
     91 	if (inb(iobase + LMS_SIGN) != 0x0c)
     92 		return 0;
     93 	outb(iobase + LMS_SIGN, 0x50);
     94 	delay(10);
     95 	if (inb(iobase + LMS_SIGN) != 0x50)
     96 		return 0;
     97 
     98 	/* Disable interrupts. */
     99 	outb(iobase + LMS_CNTRL, 0x10);
    100 
    101 	ia->ia_iosize = LMS_NPORTS;
    102 	ia->ia_msize = 0;
    103 	return 1;
    104 }
    105 
    106 void
    107 lmsattach(parent, self, aux)
    108 	struct device *parent, *self;
    109 	void *aux;
    110 {
    111 	struct lms_softc *sc = (void *)self;
    112 	struct isa_attach_args *ia = aux;
    113 	u_short iobase = ia->ia_iobase;
    114 
    115 	/* Other initialization was done by lmsprobe. */
    116 	sc->sc_iobase = iobase;
    117 	sc->sc_state = 0;
    118 }
    119 
    120 int
    121 lmsopen(dev, flag)
    122 	dev_t dev;
    123 	int flag;
    124 {
    125 	int unit = LMSUNIT(dev);
    126 	struct lms_softc *sc;
    127 
    128 	if (unit >= lmscd.cd_ndevs)
    129 		return ENXIO;
    130 	sc = lmscd.cd_devs[unit];
    131 	if (!sc)
    132 		return ENXIO;
    133 
    134 	if (sc->sc_state & LMS_OPEN)
    135 		return EBUSY;
    136 
    137 	if (clalloc(&sc->sc_q, LMS_BSIZE, 0) == -1)
    138 		return ENOMEM;
    139 
    140 	sc->sc_state |= LMS_OPEN;
    141 	sc->sc_status = 0;
    142 	sc->sc_x = sc->sc_y = 0;
    143 
    144 	/* Enable interrupts. */
    145 	outb(sc->sc_iobase + LMS_CNTRL, 0);
    146 
    147 	return 0;
    148 }
    149 
    150 int
    151 lmsclose(dev, flag)
    152 	dev_t dev;
    153 	int flag;
    154 {
    155 	struct lms_softc *sc = lmscd.cd_devs[LMSUNIT(dev)];
    156 
    157 	/* Disable interrupts. */
    158 	outb(sc->sc_iobase + LMS_CNTRL, 0x10);
    159 
    160 	sc->sc_state &= ~LMS_OPEN;
    161 
    162 	clfree(&sc->sc_q);
    163 
    164 	return 0;
    165 }
    166 
    167 int
    168 lmsread(dev, uio, flag)
    169 	dev_t dev;
    170 	struct uio *uio;
    171 	int flag;
    172 {
    173 	struct lms_softc *sc = lmscd.cd_devs[LMSUNIT(dev)];
    174 	int s;
    175 	int error;
    176 	size_t length;
    177 	u_char buffer[LMS_CHUNK];
    178 
    179 	/* Block until mouse activity occured. */
    180 
    181 	s = spltty();
    182 	while (sc->sc_q.c_cc == 0) {
    183 		if (flag & IO_NDELAY) {
    184 			splx(s);
    185 			return EWOULDBLOCK;
    186 		}
    187 		sc->sc_state |= LMS_ASLP;
    188 		if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0)) {
    189 			sc->sc_state &= ~LMS_ASLP;
    190 			splx(s);
    191 			return error;
    192 		}
    193 	}
    194 	splx(s);
    195 
    196 	/* Transfer as many chunks as possible. */
    197 
    198 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    199 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    200 		if (length > sizeof(buffer))
    201 			length = sizeof(buffer);
    202 
    203 		/* Remove a small chunk from the input queue. */
    204 		(void) q_to_b(&sc->sc_q, buffer, length);
    205 
    206 		/* Copy the data to the user process. */
    207 		if (error = uiomove(buffer, length, uio))
    208 			break;
    209 	}
    210 
    211 	return error;
    212 }
    213 
    214 int
    215 lmsioctl(dev, cmd, addr, flag)
    216 	dev_t dev;
    217 	int cmd;
    218 	caddr_t addr;
    219 	int flag;
    220 {
    221 	struct lms_softc *sc = lmscd.cd_devs[LMSUNIT(dev)];
    222 	struct mouseinfo info;
    223 	int s;
    224 	int error;
    225 
    226 	switch (cmd) {
    227 	case MOUSEIOCREAD:
    228 		s = spltty();
    229 
    230 		info.status = sc->sc_status;
    231 		if (sc->sc_x || sc->sc_y)
    232 			info.status |= MOVEMENT;
    233 
    234 		if (sc->sc_x > 127)
    235 			info.xmotion = 127;
    236 		else if (sc->sc_x < -127)
    237 			/* Bounding at -127 avoids a bug in XFree86. */
    238 			info.xmotion = -127;
    239 		else
    240 			info.xmotion = sc->sc_x;
    241 
    242 		if (sc->sc_y > 127)
    243 			info.ymotion = 127;
    244 		else if (sc->sc_y < -127)
    245 			info.ymotion = -127;
    246 		else
    247 			info.ymotion = sc->sc_y;
    248 
    249 		/* Reset historical information. */
    250 		sc->sc_x = sc->sc_y = 0;
    251 		sc->sc_status &= ~BUTCHNGMASK;
    252 		flushq(&sc->sc_q);
    253 
    254 		splx(s);
    255 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    256 		break;
    257 
    258 	default:
    259 		error = EINVAL;
    260 		break;
    261 	}
    262 
    263 	return error;
    264 }
    265 
    266 int
    267 lmsintr(unit)
    268 	int unit;
    269 {
    270 	struct lms_softc *sc = lmscd.cd_devs[unit];
    271 	u_short iobase = sc->sc_iobase;
    272 	u_char hi, lo, buttons, changed;
    273 	char dx, dy;
    274 	u_char buffer[5];
    275 
    276 	if ((sc->sc_state & LMS_OPEN) == 0)
    277 		/* Interrupts are not expected. */
    278 		return 0;
    279 
    280 	outb(iobase + LMS_CNTRL, 0xab);
    281 	hi = inb(iobase + LMS_DATA);
    282 	outb(iobase + LMS_CNTRL, 0x90);
    283 	lo = inb(iobase + LMS_DATA);
    284 	dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
    285 	/* Bounding at -127 avoids a bug in XFree86. */
    286 	dx = (dx == -128) ? -127 : dx;
    287 
    288 	outb(iobase + LMS_CNTRL, 0xf0);
    289 	hi = inb(iobase + LMS_DATA);
    290 	outb(iobase + LMS_CNTRL, 0xd0);
    291 	lo = inb(iobase + LMS_DATA);
    292 	dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
    293 	dy = (dy == -128) ? 127 : -dy;
    294 
    295 	outb(iobase + LMS_CNTRL, 0);
    296 
    297 	buttons = (~hi >> 5) & 0x07;
    298 	changed = ((buttons ^ sc->sc_status) & 0x07) << 3;
    299 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    300 
    301 	if (dx || dy || changed) {
    302 		/* Update accumulated movements. */
    303 		sc->sc_x += dx;
    304 		sc->sc_y += dy;
    305 
    306 		/* Add this event to the queue. */
    307 		buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
    308 		buffer[1] = dx;
    309 		buffer[2] = dy;
    310 		buffer[3] = buffer[4] = 0;
    311 		(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    312 
    313 		if (sc->sc_state & LMS_ASLP) {
    314 			sc->sc_state &= ~LMS_ASLP;
    315 			wakeup((caddr_t)sc);
    316 		}
    317 		selwakeup(&sc->sc_rsel);
    318 	}
    319 
    320 	return 1;
    321 }
    322 
    323 int
    324 lmsselect(dev, rw, p)
    325 	dev_t dev;
    326 	int rw;
    327 	struct proc *p;
    328 {
    329 	struct lms_softc *sc = lmscd.cd_devs[LMSUNIT(dev)];
    330 	int s;
    331 	int ret;
    332 
    333 	if (rw == FWRITE)
    334 		return 0;
    335 
    336 	s = spltty();
    337 	if (!sc->sc_q.c_cc) {
    338 		selrecord(p, &sc->sc_rsel);
    339 		ret = 0;
    340 	} else
    341 		ret = 1;
    342 	splx(s);
    343 
    344 	return ret;
    345 }
    346