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