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