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