Home | History | Annotate | Line # | Download | only in isa
mms.c revision 1.30
      1 /*	$NetBSD: mms.c,v 1.30 1998/01/12 18:59:14 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 extern struct cfdriver mms_cd;
     81 
     82 #define	MMSUNIT(dev)	(minor(dev))
     83 
     84 int
     85 mmsprobe(parent, match, aux)
     86 	struct device *parent;
     87 	void *match, *aux;
     88 {
     89 	struct isa_attach_args *ia = aux;
     90 	bus_space_tag_t iot = ia->ia_iot;
     91 	bus_space_handle_t ioh;
     92 	int rv;
     93 
     94 	/* Disallow wildcarded i/o address. */
     95 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
     96 		return 0;
     97 
     98 	/* Map the i/o space. */
     99 	if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh))
    100 		return 0;
    101 
    102 	rv = 0;
    103 
    104 	/* Read identification register to see if present */
    105 	if (bus_space_read_1(iot, ioh, MMS_IDENT) != 0xde)
    106 		goto out;
    107 
    108 	/* Seems it was there; reset. */
    109 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x87);
    110 
    111 	rv = 1;
    112 	ia->ia_iosize = MMS_NPORTS;
    113 	ia->ia_msize = 0;
    114 
    115 out:
    116 	bus_space_unmap(iot, ioh, MMS_NPORTS);
    117 	return rv;
    118 }
    119 
    120 void
    121 mmsattach(parent, self, aux)
    122 	struct device *parent, *self;
    123 	void *aux;
    124 {
    125 	struct mms_softc *sc = (void *)self;
    126 	struct isa_attach_args *ia = aux;
    127 	bus_space_tag_t iot = ia->ia_iot;
    128 	bus_space_handle_t ioh;
    129 
    130 	printf("\n");
    131 
    132 	if (bus_space_map(iot, ia->ia_iobase, MMS_NPORTS, 0, &ioh)) {
    133 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    134 		return;
    135 	}
    136 
    137 	/* Other initialization was done by mmsprobe. */
    138 	sc->sc_iot = iot;
    139 	sc->sc_ioh = ioh;
    140 	sc->sc_state = 0;
    141 
    142 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
    143 	    IPL_TTY, mmsintr, sc);
    144 }
    145 
    146 int
    147 mmsopen(dev, flag, mode, p)
    148 	dev_t dev;
    149 	int flag;
    150 	int mode;
    151 	struct proc *p;
    152 {
    153 	int unit = MMSUNIT(dev);
    154 	struct mms_softc *sc;
    155 
    156 	if (unit >= mms_cd.cd_ndevs)
    157 		return ENXIO;
    158 	sc = mms_cd.cd_devs[unit];
    159 	if (!sc)
    160 		return ENXIO;
    161 
    162 	if (sc->sc_state & MMS_OPEN)
    163 		return EBUSY;
    164 
    165 	if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1)
    166 		return ENOMEM;
    167 
    168 	sc->sc_state |= MMS_OPEN;
    169 	sc->sc_status = 0;
    170 	sc->sc_x = sc->sc_y = 0;
    171 
    172 	/* Enable interrupts. */
    173 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x07);
    174 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_DATA, 0x09);
    175 
    176 	return 0;
    177 }
    178 
    179 int
    180 mmsclose(dev, flag, mode, p)
    181 	dev_t dev;
    182 	int flag;
    183 	int mode;
    184 	struct proc *p;
    185 {
    186 	struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
    187 
    188 	/* Disable interrupts. */
    189 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MMS_ADDR, 0x87);
    190 
    191 	sc->sc_state &= ~MMS_OPEN;
    192 
    193 	clfree(&sc->sc_q);
    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 	struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
    205 	int s;
    206 	int error = 0;
    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 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0);
    220 		if (error) {
    221 			sc->sc_state &= ~MMS_ASLP;
    222 			splx(s);
    223 			return error;
    224 		}
    225 	}
    226 	splx(s);
    227 
    228 	/* Transfer as many chunks as possible. */
    229 
    230 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    231 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    232 		if (length > sizeof(buffer))
    233 			length = sizeof(buffer);
    234 
    235 		/* Remove a small chunk from the input queue. */
    236 		(void) q_to_b(&sc->sc_q, buffer, length);
    237 
    238 		/* Copy the data to the user process. */
    239 		if ((error = uiomove(buffer, length, uio)) != 0)
    240 			break;
    241 	}
    242 
    243 	return error;
    244 }
    245 
    246 int
    247 mmsioctl(dev, cmd, addr, flag, p)
    248 	dev_t dev;
    249 	u_long cmd;
    250 	caddr_t addr;
    251 	int flag;
    252 	struct proc *p;
    253 {
    254 	struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
    255 	struct mouseinfo info;
    256 	int s;
    257 	int error;
    258 
    259 	switch (cmd) {
    260 	case MOUSEIOCREAD:
    261 		s = spltty();
    262 
    263 		info.status = sc->sc_status;
    264 		if (sc->sc_x || sc->sc_y)
    265 			info.status |= MOVEMENT;
    266 
    267 		if (sc->sc_x > 127)
    268 			info.xmotion = 127;
    269 		else if (sc->sc_x < -127)
    270 			/* Bounding at -127 avoids a bug in XFree86. */
    271 			info.xmotion = -127;
    272 		else
    273 			info.xmotion = sc->sc_x;
    274 
    275 		if (sc->sc_y > 127)
    276 			info.ymotion = 127;
    277 		else if (sc->sc_y < -127)
    278 			info.ymotion = -127;
    279 		else
    280 			info.ymotion = sc->sc_y;
    281 
    282 		/* Reset historical information. */
    283 		sc->sc_x = sc->sc_y = 0;
    284 		sc->sc_status &= ~BUTCHNGMASK;
    285 		ndflush(&sc->sc_q, sc->sc_q.c_cc);
    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 = arg;
    304 	bus_space_tag_t iot = sc->sc_iot;
    305 	bus_space_handle_t ioh = sc->sc_ioh;
    306 	u_char buttons, changed, status;
    307 	char dx, dy;
    308 	u_char buffer[5];
    309 
    310 	if ((sc->sc_state & MMS_OPEN) == 0)
    311 		/* Interrupts are not expected. */
    312 		return 0;
    313 
    314 	/* Freeze InPort registers (disabling interrupts). */
    315 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
    316 	bus_space_write_1(iot, ioh, MMS_DATA, 0x29);
    317 
    318 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x00);
    319 	status = bus_space_read_1(iot, ioh, MMS_DATA);
    320 
    321 	if (status & 0x40) {
    322 		bus_space_write_1(iot, ioh, MMS_ADDR, 1);
    323 		dx = bus_space_read_1(iot, ioh, MMS_DATA);
    324 		dx = (dx == -128) ? -127 : dx;
    325 		bus_space_write_1(iot, ioh, MMS_ADDR, 2);
    326 		dy = bus_space_read_1(iot, ioh, MMS_DATA);
    327 		dy = (dy == -128) ? 127 : -dy;
    328 	} else
    329 		dx = dy = 0;
    330 
    331 	/* Unfreeze InPort registers (reenabling interrupts). */
    332 	bus_space_write_1(iot, ioh, MMS_ADDR, 0x07);
    333 	bus_space_write_1(iot, ioh, MMS_DATA, 0x09);
    334 
    335 	buttons = status & BUTSTATMASK;
    336 	changed = status & BUTCHNGMASK;
    337 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    338 
    339 	if (dx || dy || changed) {
    340 		/* Update accumulated movements. */
    341 		sc->sc_x += dx;
    342 		sc->sc_y += dy;
    343 
    344 		/* Add this event to the queue. */
    345 		buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
    346 		buffer[1] = dx;
    347 		buffer[2] = dy;
    348 		buffer[3] = buffer[4] = 0;
    349 		(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    350 
    351 		if (sc->sc_state & MMS_ASLP) {
    352 			sc->sc_state &= ~MMS_ASLP;
    353 			wakeup((caddr_t)sc);
    354 		}
    355 		selwakeup(&sc->sc_rsel);
    356 	}
    357 
    358 	return -1;
    359 }
    360 
    361 int
    362 mmspoll(dev, events, p)
    363 	dev_t dev;
    364 	int events;
    365 	struct proc *p;
    366 {
    367 	struct mms_softc *sc = mms_cd.cd_devs[MMSUNIT(dev)];
    368 	int revents = 0;
    369 	int s = spltty();
    370 
    371 	if (events & (POLLIN | POLLRDNORM))
    372 		if (sc->sc_q.c_cc > 0)
    373 			revents |= events & (POLLIN | POLLRDNORM);
    374 		else
    375 			selrecord(p, &sc->sc_rsel);
    376 
    377 	splx(s);
    378 	return (revents);
    379 }
    380