Home | History | Annotate | Line # | Download | only in isa
mms.c revision 1.28
      1 /*	$NetBSD: mms.c,v 1.28 1997/10/19 19:17:07 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994 Charles Hannum.
      5  * Copyright (c) 1992, 1993 Erik Forsberg.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
     15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
     17  * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     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 #include <sys/poll.h>
     39 
     40 #include <machine/cpu.h>
     41 #include <machine/intr.h>
     42 #include <machine/bus.h>
     43 #include <machine/mouse.h>
     44 #include <machine/conf.h>
     45 
     46 #include <dev/isa/isavar.h>
     47 
     48 #define	MMS_ADDR	0	/* offset for register select */
     49 #define	MMS_DATA	1	/* offset for InPort data */
     50 #define	MMS_IDENT	2	/* offset for identification register */
     51 #define	MMS_NPORTS	4
     52 
     53 #define	MMS_CHUNK	128	/* chunk size for read */
     54 #define	MMS_BSIZE	1020	/* buffer size */
     55 
     56 struct mms_softc {		/* driver status information */
     57 	struct device sc_dev;
     58 	void *sc_ih;
     59 
     60 	bus_space_tag_t sc_iot;
     61 	bus_space_handle_t sc_ioh;
     62 
     63 	struct clist sc_q;
     64 	struct selinfo sc_rsel;
     65 	u_char sc_state;	/* mouse driver state */
     66 #define	MMS_OPEN	0x01	/* device is open */
     67 #define	MMS_ASLP	0x02	/* waiting for mouse data */
     68 	u_char sc_status;	/* mouse button status */
     69 	int sc_x, sc_y;		/* accumulated motion in the X,Y axis */
     70 };
     71 
     72 int mmsprobe __P((struct device *, void *, void *));
     73 void mmsattach __P((struct device *, struct device *, void *));
     74 int mmsintr __P((void *));
     75 
     76 struct cfattach mms_ca = {
     77 	sizeof(struct mms_softc), mmsprobe, mmsattach
     78 };
     79 
     80 struct cfdriver mms_cd = {
     81 	NULL, "mms", DV_TTY
     82 };
     83 
     84 #define	MMSUNIT(dev)	(minor(dev))
     85 
     86 int
     87 mmsprobe(parent, match, aux)
     88 	struct device *parent;
     89 	void *match, *aux;
     90 {
     91 	struct isa_attach_args *ia = aux;
     92 	bus_space_tag_t iot = ia->ia_iot;
     93 	bus_space_handle_t ioh;
     94 	int rv = 0;
     95 
     96 	/* Disallow wildcarded i/o address. */
     97 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
     98 		return 0;
     99 
    100 	if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh))
    101 		return 0;
    102 
    103 	/* Read identification register to see if present */
    104 	if (bus_space_read_1(iot, ioh, MMS_IDENT) != 0xde)
    105 		goto out;
    106 
    107 	/* Seems it was there; reset. */
    108 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x87);
    109 
    110 	rv = 1;
    111 	ia->ia_iosize = MMS_NPORTS;
    112 	ia->ia_msize = 0;
    113  out:
    114 	bus_space_unmap(iot, ioh, MMS_NPORTS);
    115 	return rv;
    116 }
    117 
    118 void
    119 mmsattach(parent, self, aux)
    120 	struct device *parent, *self;
    121 	void *aux;
    122 {
    123 	struct mms_softc *sc = (void *)self;
    124 	struct isa_attach_args *ia = aux;
    125 	bus_space_tag_t iot = ia->ia_iot;
    126 	bus_space_handle_t ioh;
    127 
    128 	printf("\n");
    129 
    130 	if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh)) {
    131 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    132 		return;
    133 	}
    134 
    135 	/* Other initialization was done by mmsprobe. */
    136 	sc->sc_iot = iot;
    137 	sc->sc_ioh = ioh;
    138 	sc->sc_state = 0;
    139 
    140 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
    141 	    IPL_TTY, mmsintr, sc);
    142 }
    143 
    144 int
    145 mmsopen(dev, flag, mode, p)
    146 	dev_t dev;
    147 	int flag;
    148 	int mode;
    149 	struct proc *p;
    150 {
    151 	int unit = MMSUNIT(dev);
    152 	struct mms_softc *sc;
    153 
    154 	if (unit >= mms_cd.cd_ndevs)
    155 		return ENXIO;
    156 	sc = mms_cd.cd_devs[unit];
    157 	if (!sc)
    158 		return ENXIO;
    159 
    160 	if (sc->sc_state & MMS_OPEN)
    161 		return EBUSY;
    162 
    163 	if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1)
    164 		return ENOMEM;
    165 
    166 	sc->sc_state |= MMS_OPEN;
    167 	sc->sc_status = 0;
    168 	sc->sc_x = sc->sc_y = 0;
    169 
    170 	/* Enable interrupts. */
    171 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x07);
    172 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_DATA, 0x09);
    173 
    174 	return 0;
    175 }
    176 
    177 int
    178 mmsclose(dev, flag, mode, p)
    179 	dev_t dev;
    180 	int flag;
    181 	int mode;
    182 	struct proc *p;
    183 {
    184 	struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
    185 
    186 	/* Disable interrupts. */
    187 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x87);
    188 
    189 	sc->sc_state &= ~MMS_OPEN;
    190 
    191 	clfree(&sc->sc_q);
    192 
    193 	return 0;
    194 }
    195 
    196 int
    197 mmsread(dev, uio, flag)
    198 	dev_t dev;
    199 	struct uio *uio;
    200 	int flag;
    201 {
    202 	struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
    203 	int s;
    204 	int error = 0;
    205 	size_t length;
    206 	u_char buffer[MMS_CHUNK];
    207 
    208 	/* Block until mouse activity occured. */
    209 
    210 	s = spltty();
    211 	while (sc->sc_q.c_cc == 0) {
    212 		if (flag & IO_NDELAY) {
    213 			splx(s);
    214 			return EWOULDBLOCK;
    215 		}
    216 		sc->sc_state |= MMS_ASLP;
    217 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0);
    218 		if (error) {
    219 			sc->sc_state &= ~MMS_ASLP;
    220 			splx(s);
    221 			return error;
    222 		}
    223 	}
    224 	splx(s);
    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 the input queue. */
    234 		(void) q_to_b(&sc->sc_q, buffer, length);
    235 
    236 		/* Copy the data to the user process. */
    237 		if ((error = uiomove(buffer, length, uio)) != 0)
    238 			break;
    239 	}
    240 
    241 	return error;
    242 }
    243 
    244 int
    245 mmsioctl(dev, cmd, addr, flag, p)
    246 	dev_t dev;
    247 	u_long cmd;
    248 	caddr_t addr;
    249 	int flag;
    250 	struct proc *p;
    251 {
    252 	struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
    253 	struct mouseinfo info;
    254 	int s;
    255 	int error;
    256 
    257 	switch (cmd) {
    258 	case MOUSEIOCREAD:
    259 		s = spltty();
    260 
    261 		info.status = sc->sc_status;
    262 		if (sc->sc_x || sc->sc_y)
    263 			info.status |= MOVEMENT;
    264 
    265 		if (sc->sc_x > 127)
    266 			info.xmotion = 127;
    267 		else if (sc->sc_x < -127)
    268 			/* Bounding at -127 avoids a bug in XFree86. */
    269 			info.xmotion = -127;
    270 		else
    271 			info.xmotion = sc->sc_x;
    272 
    273 		if (sc->sc_y > 127)
    274 			info.ymotion = 127;
    275 		else if (sc->sc_y < -127)
    276 			info.ymotion = -127;
    277 		else
    278 			info.ymotion = sc->sc_y;
    279 
    280 		/* Reset historical information. */
    281 		sc->sc_x = sc->sc_y = 0;
    282 		sc->sc_status &= ~BUTCHNGMASK;
    283 		ndflush(&sc->sc_q, sc->sc_q.c_cc);
    284 
    285 		splx(s);
    286 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    287 		break;
    288 
    289 	default:
    290 		error = EINVAL;
    291 		break;
    292 	}
    293 
    294 	return error;
    295 }
    296 
    297 int
    298 mmsintr(arg)
    299 	void *arg;
    300 {
    301 	struct mms_softc *sc = arg;
    302 	u_char buttons, changed, status;
    303 	char dx, dy;
    304 	u_char buffer[5];
    305 	bus_space_tag_t iot = sc->sc_iot;
    306 	bus_space_handle_t ioh = sc->sc_ioh;
    307 
    308 	if ((sc->sc_state & MMS_OPEN) == 0)
    309 		/* Interrupts are not expected. */
    310 		return 0;
    311 
    312 	/* Freeze InPort registers (disabling interrupts). */
    313 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
    314 	bus_space_write_1(iot, ioh, MMS_DATA, 0x29);
    315 
    316 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x00);
    317 	status = bus_space_read_1(iot, ioh, MMS_DATA);
    318 
    319 	if (status & 0x40) {
    320 		bus_space_write_1(iot, ioh, MMS_ADDR, 1);
    321 		dx = bus_space_read_1(iot, ioh, MMS_DATA);
    322 		dx = (dx == -128) ? -127 : dx;
    323 		bus_space_write_1(iot, ioh, MMS_ADDR, 2);
    324 		dy = bus_space_read_1(iot, ioh, MMS_DATA);
    325 		dy = (dy == -128) ? 127 : -dy;
    326 	} else
    327 		dx = dy = 0;
    328 
    329 	/* Unfreeze InPort registers (reenabling interrupts). */
    330 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
    331 	bus_space_write_1(iot, ioh, MMS_DATA, 0x09);
    332 
    333 	buttons = status & BUTSTATMASK;
    334 	changed = status & BUTCHNGMASK;
    335 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    336 
    337 	if (dx || dy || changed) {
    338 		/* Update accumulated movements. */
    339 		sc->sc_x += dx;
    340 		sc->sc_y += dy;
    341 
    342 		/* Add this event to the queue. */
    343 		buffer[0] = 0x80 | (buttons ^ 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 mmspoll(dev, events, p)
    361 	dev_t dev;
    362 	int events;
    363 	struct proc *p;
    364 {
    365 	struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
    366 	int revents = 0;
    367 	int s = spltty();
    368 
    369 	if (events & (POLLIN | POLLRDNORM))
    370 		if (sc->sc_q.c_cc > 0)
    371 			revents |= events & (POLLIN | POLLRDNORM);
    372 		else
    373 			selrecord(p, &sc->sc_rsel);
    374 
    375 	splx(s);
    376 	return (revents);
    377 }
    378