Home | History | Annotate | Line # | Download | only in isa
lms.c revision 1.30
      1 /*	$NetBSD: lms.c,v 1.30 1996/10/21 22:27:41 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/bus.h>
     42 #include <machine/intr.h>
     43 #include <machine/mouse.h>
     44 #include <machine/conf.h>
     45 
     46 #include <dev/isa/isavar.h>
     47 
     48 #define	LMS_DATA	0       /* offset for data port, read-only */
     49 #define	LMS_SIGN	1       /* offset for signature port, read-write */
     50 #define	LMS_INTR	2       /* offset for interrupt port, read-only */
     51 #define	LMS_CNTRL	2       /* offset for control port, write-only */
     52 #define	LMS_CONFIG	3	/* for configuration port, read-write */
     53 #define	LMS_NPORTS	4
     54 
     55 #define	LMS_CHUNK	128	/* chunk size for read */
     56 #define	LMS_BSIZE	1020	/* buffer size */
     57 
     58 struct lms_softc {		/* driver status information */
     59 	struct device sc_dev;
     60 	void *sc_ih;
     61 
     62 	bus_space_tag_t sc_iot;		/* bus i/o space identifier */
     63 	bus_space_handle_t sc_ioh;	/* bus i/o handle */
     64 
     65 	struct clist sc_q;
     66 	struct selinfo sc_rsel;
     67 	u_char sc_state;	/* mouse driver state */
     68 #define	LMS_OPEN	0x01	/* device is open */
     69 #define	LMS_ASLP	0x02	/* waiting for mouse data */
     70 	u_char sc_status;	/* mouse button status */
     71 	int sc_x, sc_y;		/* accumulated motion in the X,Y axis */
     72 };
     73 
     74 int lmsprobe __P((struct device *, void *, void *));
     75 void lmsattach __P((struct device *, struct device *, void *));
     76 int lmsintr __P((void *));
     77 
     78 struct cfattach lms_ca = {
     79 	sizeof(struct lms_softc), lmsprobe, lmsattach
     80 };
     81 
     82 struct cfdriver lms_cd = {
     83 	NULL, "lms", DV_TTY
     84 };
     85 
     86 #define	LMSUNIT(dev)	(minor(dev))
     87 
     88 int
     89 lmsprobe(parent, match, aux)
     90 	struct device *parent;
     91 	void *match, *aux;
     92 {
     93 	struct isa_attach_args *ia = aux;
     94 	bus_space_tag_t iot = ia->ia_iot;
     95 	bus_space_handle_t ioh;
     96 	int rv;
     97 
     98 	/* Map the i/o space. */
     99 	if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh))
    100 		return 0;
    101 
    102 	rv = 0;
    103 
    104 	/* Configure and check for port present. */
    105 	bus_space_write_1(iot, ioh, LMS_CONFIG, 0x91);
    106 	delay(10);
    107 	bus_space_write_1(iot, ioh, LMS_SIGN, 0x0c);
    108 	delay(10);
    109 	if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x0c)
    110 		goto out;
    111 	bus_space_write_1(iot, ioh, LMS_SIGN, 0x50);
    112 	delay(10);
    113 	if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x50)
    114 		goto out;
    115 
    116 	/* Disable interrupts. */
    117 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0x10);
    118 
    119 	rv = 1;
    120 	ia->ia_iosize = LMS_NPORTS;
    121 	ia->ia_msize = 0;
    122 
    123 out:
    124 	bus_space_unmap(iot, ioh, LMS_NPORTS);
    125 	return rv;
    126 }
    127 
    128 void
    129 lmsattach(parent, self, aux)
    130 	struct device *parent, *self;
    131 	void *aux;
    132 {
    133 	struct lms_softc *sc = (void *)self;
    134 	struct isa_attach_args *ia = aux;
    135 
    136 	printf("\n");
    137 
    138 	/* Other initialization was done by lmsprobe. */
    139 	sc->sc_iot = ia->ia_iot;
    140 	if (bus_space_map(sc->sc_iot, ia->ia_iobase, LMS_NPORTS, 0,
    141 	    &sc->sc_ioh))
    142 		panic("lmsattach: couldn't map I/O ports");
    143 
    144 	sc->sc_state = 0;
    145 
    146 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
    147 	    IPL_TTY, lmsintr, sc);
    148 }
    149 
    150 int
    151 lmsopen(dev, flag, mode, p)
    152 	dev_t dev;
    153 	int flag;
    154 	int mode;
    155 	struct proc *p;
    156 {
    157 	int unit = LMSUNIT(dev);
    158 	struct lms_softc *sc;
    159 
    160 	if (unit >= lms_cd.cd_ndevs)
    161 		return ENXIO;
    162 	sc = lms_cd.cd_devs[unit];
    163 	if (!sc)
    164 		return ENXIO;
    165 
    166 	if (sc->sc_state & LMS_OPEN)
    167 		return EBUSY;
    168 
    169 	if (clalloc(&sc->sc_q, LMS_BSIZE, 0) == -1)
    170 		return ENOMEM;
    171 
    172 	sc->sc_state |= LMS_OPEN;
    173 	sc->sc_status = 0;
    174 	sc->sc_x = sc->sc_y = 0;
    175 
    176 	/* Enable interrupts. */
    177 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0);
    178 
    179 	return 0;
    180 }
    181 
    182 int
    183 lmsclose(dev, flag, mode, p)
    184 	dev_t dev;
    185 	int flag;
    186 	int mode;
    187 	struct proc *p;
    188 {
    189 	struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
    190 
    191 	/* Disable interrupts. */
    192 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0x10);
    193 
    194 	sc->sc_state &= ~LMS_OPEN;
    195 
    196 	clfree(&sc->sc_q);
    197 
    198 	return 0;
    199 }
    200 
    201 int
    202 lmsread(dev, uio, flag)
    203 	dev_t dev;
    204 	struct uio *uio;
    205 	int flag;
    206 {
    207 	struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
    208 	int s;
    209 	int error = 0;
    210 	size_t length;
    211 	u_char buffer[LMS_CHUNK];
    212 
    213 	/* Block until mouse activity occured. */
    214 
    215 	s = spltty();
    216 	while (sc->sc_q.c_cc == 0) {
    217 		if (flag & IO_NDELAY) {
    218 			splx(s);
    219 			return EWOULDBLOCK;
    220 		}
    221 		sc->sc_state |= LMS_ASLP;
    222 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0);
    223 		if (error) {
    224 			sc->sc_state &= ~LMS_ASLP;
    225 			splx(s);
    226 			return error;
    227 		}
    228 	}
    229 	splx(s);
    230 
    231 	/* Transfer as many chunks as possible. */
    232 
    233 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    234 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    235 		if (length > sizeof(buffer))
    236 			length = sizeof(buffer);
    237 
    238 		/* Remove a small chunk from the input queue. */
    239 		(void) q_to_b(&sc->sc_q, buffer, length);
    240 
    241 		/* Copy the data to the user process. */
    242 		if ((error = uiomove(buffer, length, uio)) != 0)
    243 			break;
    244 	}
    245 
    246 	return error;
    247 }
    248 
    249 int
    250 lmsioctl(dev, cmd, addr, flag, p)
    251 	dev_t dev;
    252 	u_long cmd;
    253 	caddr_t addr;
    254 	int flag;
    255 	struct proc *p;
    256 {
    257 	struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
    258 	struct mouseinfo info;
    259 	int s;
    260 	int error;
    261 
    262 	switch (cmd) {
    263 	case MOUSEIOCREAD:
    264 		s = spltty();
    265 
    266 		info.status = sc->sc_status;
    267 		if (sc->sc_x || sc->sc_y)
    268 			info.status |= MOVEMENT;
    269 
    270 		if (sc->sc_x > 127)
    271 			info.xmotion = 127;
    272 		else if (sc->sc_x < -127)
    273 			/* Bounding at -127 avoids a bug in XFree86. */
    274 			info.xmotion = -127;
    275 		else
    276 			info.xmotion = sc->sc_x;
    277 
    278 		if (sc->sc_y > 127)
    279 			info.ymotion = 127;
    280 		else if (sc->sc_y < -127)
    281 			info.ymotion = -127;
    282 		else
    283 			info.ymotion = sc->sc_y;
    284 
    285 		/* Reset historical information. */
    286 		sc->sc_x = sc->sc_y = 0;
    287 		sc->sc_status &= ~BUTCHNGMASK;
    288 		ndflush(&sc->sc_q, sc->sc_q.c_cc);
    289 
    290 		splx(s);
    291 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    292 		break;
    293 
    294 	default:
    295 		error = EINVAL;
    296 		break;
    297 	}
    298 
    299 	return error;
    300 }
    301 
    302 int
    303 lmsintr(arg)
    304 	void *arg;
    305 {
    306 	struct lms_softc *sc = arg;
    307 	bus_space_tag_t iot = sc->sc_iot;
    308 	bus_space_handle_t ioh = sc->sc_ioh;
    309 	u_char hi, lo, buttons, changed;
    310 	char dx, dy;
    311 	u_char buffer[5];
    312 
    313 	if ((sc->sc_state & LMS_OPEN) == 0)
    314 		/* Interrupts are not expected. */
    315 		return 0;
    316 
    317 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xab);
    318 	hi = bus_space_read_1(iot, ioh, LMS_DATA);
    319 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0x90);
    320 	lo = bus_space_read_1(iot, ioh, LMS_DATA);
    321 	dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
    322 	/* Bounding at -127 avoids a bug in XFree86. */
    323 	dx = (dx == -128) ? -127 : dx;
    324 
    325 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xf0);
    326 	hi = bus_space_read_1(iot, ioh, LMS_DATA);
    327 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xd0);
    328 	lo = bus_space_read_1(iot, ioh, LMS_DATA);
    329 	dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
    330 	dy = (dy == -128) ? 127 : -dy;
    331 
    332 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0);
    333 
    334 	buttons = (~hi >> 5) & 0x07;
    335 	changed = ((buttons ^ sc->sc_status) & 0x07) << 3;
    336 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    337 
    338 	if (dx || dy || changed) {
    339 		/* Update accumulated movements. */
    340 		sc->sc_x += dx;
    341 		sc->sc_y += dy;
    342 
    343 		/* Add this event to the queue. */
    344 		buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
    345 		buffer[1] = dx;
    346 		buffer[2] = dy;
    347 		buffer[3] = buffer[4] = 0;
    348 		(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    349 
    350 		if (sc->sc_state & LMS_ASLP) {
    351 			sc->sc_state &= ~LMS_ASLP;
    352 			wakeup((caddr_t)sc);
    353 		}
    354 		selwakeup(&sc->sc_rsel);
    355 	}
    356 
    357 	return -1;
    358 }
    359 
    360 int
    361 lmspoll(dev, events, p)
    362 	dev_t dev;
    363 	int events;
    364 	struct proc *p;
    365 {
    366 	struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
    367 	int revents = 0;
    368 	int s = spltty();
    369 
    370 	if (events & (POLLIN | POLLRDNORM))
    371 		if (sc->sc_q.c_cc > 0)
    372 			revents |= events & (POLLIN | POLLRDNORM);
    373 		else
    374 			selrecord(p, &sc->sc_rsel);
    375 
    376 	splx(s);
    377 	return (revents);
    378 }
    379