Home | History | Annotate | Line # | Download | only in isa
lms.c revision 1.26
      1  1.26   mycroft /*	$NetBSD: lms.c,v 1.26 1996/05/12 23:12:11 mycroft Exp $	*/
      2  1.14       cgd 
      3   1.1    andrew /*-
      4   1.8   mycroft  * Copyright (c) 1993, 1994 Charles Hannum.
      5   1.1    andrew  * Copyright (c) 1992, 1993 Erik Forsberg.
      6   1.1    andrew  * All rights reserved.
      7   1.1    andrew  *
      8   1.1    andrew  * Redistribution and use in source and binary forms, with or without
      9   1.1    andrew  * modification, are permitted provided that the following conditions
     10   1.1    andrew  * are met:
     11   1.1    andrew  * 1. Redistributions of source code must retain the above copyright
     12   1.1    andrew  *    notice, this list of conditions and the following disclaimer.
     13   1.1    andrew  *
     14   1.1    andrew  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
     15   1.1    andrew  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     16   1.1    andrew  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
     17   1.1    andrew  * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18   1.1    andrew  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19   1.1    andrew  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20   1.1    andrew  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     21   1.1    andrew  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     22   1.1    andrew  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     23   1.1    andrew  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24   1.1    andrew  */
     25   1.1    andrew 
     26   1.7   mycroft #include <sys/param.h>
     27   1.7   mycroft #include <sys/kernel.h>
     28   1.7   mycroft #include <sys/systm.h>
     29   1.7   mycroft #include <sys/buf.h>
     30   1.7   mycroft #include <sys/malloc.h>
     31   1.7   mycroft #include <sys/ioctl.h>
     32   1.7   mycroft #include <sys/tty.h>
     33   1.7   mycroft #include <sys/file.h>
     34   1.7   mycroft #include <sys/select.h>
     35   1.7   mycroft #include <sys/proc.h>
     36   1.7   mycroft #include <sys/vnode.h>
     37   1.8   mycroft #include <sys/device.h>
     38   1.7   mycroft 
     39   1.8   mycroft #include <machine/cpu.h>
     40  1.23   thorpej #include <machine/bus.h>
     41  1.26   mycroft #include <machine/intr.h>
     42   1.7   mycroft #include <machine/mouse.h>
     43  1.25  christos #include <machine/conf.h>
     44   1.1    andrew 
     45  1.19       cgd #include <dev/isa/isavar.h>
     46   1.1    andrew 
     47   1.8   mycroft #define	LMS_DATA	0       /* offset for data port, read-only */
     48   1.8   mycroft #define	LMS_SIGN	1       /* offset for signature port, read-write */
     49   1.8   mycroft #define	LMS_INTR	2       /* offset for interrupt port, read-only */
     50   1.8   mycroft #define	LMS_CNTRL	2       /* offset for control port, write-only */
     51   1.8   mycroft #define	LMS_CONFIG	3	/* for configuration port, read-write */
     52   1.8   mycroft #define	LMS_NPORTS	4
     53   1.8   mycroft 
     54   1.8   mycroft #define	LMS_CHUNK	128	/* chunk size for read */
     55   1.8   mycroft #define	LMS_BSIZE	1020	/* buffer size */
     56   1.8   mycroft 
     57   1.8   mycroft struct lms_softc {		/* driver status information */
     58   1.8   mycroft 	struct device sc_dev;
     59  1.19       cgd 	void *sc_ih;
     60   1.8   mycroft 
     61  1.23   thorpej 	bus_chipset_tag_t sc_bc;	/* bus chipset identifier */
     62  1.23   thorpej 	bus_io_handle_t sc_ioh;		/* bus i/o handle */
     63  1.23   thorpej 
     64   1.8   mycroft 	struct clist sc_q;
     65   1.8   mycroft 	struct selinfo sc_rsel;
     66   1.8   mycroft 	u_char sc_state;	/* mouse driver state */
     67   1.8   mycroft #define	LMS_OPEN	0x01	/* device is open */
     68   1.8   mycroft #define	LMS_ASLP	0x02	/* waiting for mouse data */
     69   1.8   mycroft 	u_char sc_status;	/* mouse button status */
     70   1.8   mycroft 	int sc_x, sc_y;		/* accumulated motion in the X,Y axis */
     71  1.10   mycroft };
     72   1.1    andrew 
     73  1.16   mycroft int lmsprobe __P((struct device *, void *, void *));
     74  1.16   mycroft void lmsattach __P((struct device *, struct device *, void *));
     75  1.19       cgd int lmsintr __P((void *));
     76   1.1    andrew 
     77  1.22   thorpej struct cfattach lms_ca = {
     78  1.22   thorpej 	sizeof(struct lms_softc), lmsprobe, lmsattach
     79  1.22   thorpej };
     80  1.22   thorpej 
     81  1.22   thorpej struct cfdriver lms_cd = {
     82  1.22   thorpej 	NULL, "lms", DV_TTY
     83  1.10   mycroft };
     84   1.1    andrew 
     85   1.8   mycroft #define	LMSUNIT(dev)	(minor(dev))
     86   1.8   mycroft 
     87   1.8   mycroft int
     88  1.16   mycroft lmsprobe(parent, match, aux)
     89  1.16   mycroft 	struct device *parent;
     90  1.16   mycroft 	void *match, *aux;
     91   1.1    andrew {
     92  1.10   mycroft 	struct isa_attach_args *ia = aux;
     93  1.23   thorpej 	bus_chipset_tag_t bc = ia->ia_bc;
     94  1.23   thorpej 	bus_io_handle_t ioh;
     95  1.23   thorpej 	int rv;
     96  1.23   thorpej 
     97  1.23   thorpej 	/* Map the i/o space. */
     98  1.23   thorpej 	if (bus_io_map(bc, ia->ia_iobase, LMS_NPORTS, &ioh))
     99  1.23   thorpej 		return 0;
    100  1.23   thorpej 
    101  1.23   thorpej 	rv = 0;
    102   1.1    andrew 
    103   1.8   mycroft 	/* Configure and check for port present. */
    104  1.23   thorpej 	bus_io_write_1(bc, ioh, LMS_CONFIG, 0x91);
    105   1.9   mycroft 	delay(10);
    106  1.23   thorpej 	bus_io_write_1(bc, ioh, LMS_SIGN, 0x0c);
    107   1.9   mycroft 	delay(10);
    108  1.23   thorpej 	if (bus_io_read_1(bc, ioh, LMS_SIGN) != 0x0c)
    109  1.23   thorpej 		goto out;
    110  1.23   thorpej 	bus_io_write_1(bc, ioh, LMS_SIGN, 0x50);
    111   1.9   mycroft 	delay(10);
    112  1.23   thorpej 	if (bus_io_read_1(bc, ioh, LMS_SIGN) != 0x50)
    113  1.23   thorpej 		goto out;
    114   1.1    andrew 
    115   1.8   mycroft 	/* Disable interrupts. */
    116  1.23   thorpej 	bus_io_write_1(bc, ioh, LMS_CNTRL, 0x10);
    117   1.1    andrew 
    118  1.23   thorpej 	rv = 1;
    119  1.10   mycroft 	ia->ia_iosize = LMS_NPORTS;
    120  1.10   mycroft 	ia->ia_msize = 0;
    121  1.23   thorpej 
    122  1.23   thorpej out:
    123  1.23   thorpej 	bus_io_unmap(bc, ioh, LMS_NPORTS);
    124  1.23   thorpej 	return rv;
    125   1.1    andrew }
    126   1.1    andrew 
    127  1.10   mycroft void
    128  1.10   mycroft lmsattach(parent, self, aux)
    129  1.10   mycroft 	struct device *parent, *self;
    130  1.10   mycroft 	void *aux;
    131   1.1    andrew {
    132  1.10   mycroft 	struct lms_softc *sc = (void *)self;
    133  1.10   mycroft 	struct isa_attach_args *ia = aux;
    134  1.11   mycroft 
    135  1.11   mycroft 	printf("\n");
    136   1.1    andrew 
    137   1.8   mycroft 	/* Other initialization was done by lmsprobe. */
    138  1.23   thorpej 	sc->sc_bc = ia->ia_bc;
    139  1.23   thorpej 	if (bus_io_map(sc->sc_bc, ia->ia_iobase, LMS_NPORTS, &sc->sc_ioh))
    140  1.23   thorpej 		panic("lmsattach: couldn't map I/O ports");
    141  1.23   thorpej 
    142   1.8   mycroft 	sc->sc_state = 0;
    143  1.12   mycroft 
    144  1.24       cgd 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
    145  1.24       cgd 	    IPL_TTY, lmsintr, sc);
    146   1.1    andrew }
    147   1.1    andrew 
    148   1.8   mycroft int
    149  1.25  christos lmsopen(dev, flag, mode, p)
    150   1.8   mycroft 	dev_t dev;
    151   1.8   mycroft 	int flag;
    152  1.25  christos 	int mode;
    153  1.25  christos 	struct proc *p;
    154   1.1    andrew {
    155   1.1    andrew 	int unit = LMSUNIT(dev);
    156   1.1    andrew 	struct lms_softc *sc;
    157   1.1    andrew 
    158  1.22   thorpej 	if (unit >= lms_cd.cd_ndevs)
    159   1.8   mycroft 		return ENXIO;
    160  1.22   thorpej 	sc = lms_cd.cd_devs[unit];
    161  1.10   mycroft 	if (!sc)
    162   1.8   mycroft 		return ENXIO;
    163   1.1    andrew 
    164   1.8   mycroft 	if (sc->sc_state & LMS_OPEN)
    165   1.8   mycroft 		return EBUSY;
    166   1.1    andrew 
    167   1.8   mycroft 	if (clalloc(&sc->sc_q, LMS_BSIZE, 0) == -1)
    168   1.8   mycroft 		return ENOMEM;
    169   1.1    andrew 
    170   1.8   mycroft 	sc->sc_state |= LMS_OPEN;
    171   1.8   mycroft 	sc->sc_status = 0;
    172   1.8   mycroft 	sc->sc_x = sc->sc_y = 0;
    173   1.1    andrew 
    174   1.8   mycroft 	/* Enable interrupts. */
    175  1.23   thorpej 	bus_io_write_1(sc->sc_bc, sc->sc_ioh, LMS_CNTRL, 0);
    176   1.1    andrew 
    177   1.8   mycroft 	return 0;
    178   1.1    andrew }
    179   1.1    andrew 
    180   1.8   mycroft int
    181  1.25  christos lmsclose(dev, flag, mode, p)
    182   1.8   mycroft 	dev_t dev;
    183   1.8   mycroft 	int flag;
    184  1.25  christos 	int mode;
    185  1.25  christos 	struct proc *p;
    186   1.1    andrew {
    187  1.22   thorpej 	struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
    188   1.1    andrew 
    189   1.8   mycroft 	/* Disable interrupts. */
    190  1.23   thorpej 	bus_io_write_1(sc->sc_bc, sc->sc_ioh, LMS_CNTRL, 0x10);
    191   1.1    andrew 
    192   1.8   mycroft 	sc->sc_state &= ~LMS_OPEN;
    193   1.1    andrew 
    194   1.8   mycroft 	clfree(&sc->sc_q);
    195   1.1    andrew 
    196   1.8   mycroft 	return 0;
    197   1.1    andrew }
    198   1.1    andrew 
    199   1.8   mycroft int
    200   1.8   mycroft lmsread(dev, uio, flag)
    201   1.8   mycroft 	dev_t dev;
    202   1.8   mycroft 	struct uio *uio;
    203   1.8   mycroft 	int flag;
    204   1.1    andrew {
    205  1.22   thorpej 	struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
    206   1.1    andrew 	int s;
    207  1.25  christos 	int error = 0;
    208   1.8   mycroft 	size_t length;
    209   1.8   mycroft 	u_char buffer[LMS_CHUNK];
    210   1.1    andrew 
    211   1.8   mycroft 	/* Block until mouse activity occured. */
    212   1.1    andrew 
    213   1.1    andrew 	s = spltty();
    214   1.8   mycroft 	while (sc->sc_q.c_cc == 0) {
    215   1.8   mycroft 		if (flag & IO_NDELAY) {
    216   1.1    andrew 			splx(s);
    217   1.8   mycroft 			return EWOULDBLOCK;
    218   1.1    andrew 		}
    219   1.8   mycroft 		sc->sc_state |= LMS_ASLP;
    220  1.25  christos 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0);
    221  1.25  christos 		if (error) {
    222   1.8   mycroft 			sc->sc_state &= ~LMS_ASLP;
    223   1.1    andrew 			splx(s);
    224   1.8   mycroft 			return error;
    225   1.1    andrew 		}
    226   1.1    andrew 	}
    227   1.8   mycroft 	splx(s);
    228   1.1    andrew 
    229   1.8   mycroft 	/* Transfer as many chunks as possible. */
    230   1.1    andrew 
    231   1.8   mycroft 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    232   1.8   mycroft 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    233   1.1    andrew 		if (length > sizeof(buffer))
    234   1.1    andrew 			length = sizeof(buffer);
    235   1.1    andrew 
    236   1.8   mycroft 		/* Remove a small chunk from the input queue. */
    237   1.8   mycroft 		(void) q_to_b(&sc->sc_q, buffer, length);
    238   1.1    andrew 
    239   1.8   mycroft 		/* Copy the data to the user process. */
    240  1.25  christos 		if ((error = uiomove(buffer, length, uio)) != 0)
    241   1.1    andrew 			break;
    242   1.1    andrew 	}
    243   1.1    andrew 
    244   1.8   mycroft 	return error;
    245   1.1    andrew }
    246   1.1    andrew 
    247   1.8   mycroft int
    248  1.25  christos lmsioctl(dev, cmd, addr, flag, p)
    249   1.8   mycroft 	dev_t dev;
    250  1.15       cgd 	u_long cmd;
    251   1.8   mycroft 	caddr_t addr;
    252   1.8   mycroft 	int flag;
    253  1.25  christos 	struct proc *p;
    254   1.1    andrew {
    255  1.22   thorpej 	struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
    256   1.1    andrew 	struct mouseinfo info;
    257   1.8   mycroft 	int s;
    258   1.8   mycroft 	int error;
    259   1.1    andrew 
    260   1.1    andrew 	switch (cmd) {
    261   1.1    andrew 	case MOUSEIOCREAD:
    262   1.1    andrew 		s = spltty();
    263   1.1    andrew 
    264   1.8   mycroft 		info.status = sc->sc_status;
    265   1.8   mycroft 		if (sc->sc_x || sc->sc_y)
    266   1.1    andrew 			info.status |= MOVEMENT;
    267   1.1    andrew 
    268   1.8   mycroft 		if (sc->sc_x > 127)
    269   1.1    andrew 			info.xmotion = 127;
    270   1.8   mycroft 		else if (sc->sc_x < -127)
    271   1.8   mycroft 			/* Bounding at -127 avoids a bug in XFree86. */
    272   1.3   mycroft 			info.xmotion = -127;
    273   1.1    andrew 		else
    274   1.8   mycroft 			info.xmotion = sc->sc_x;
    275   1.1    andrew 
    276   1.8   mycroft 		if (sc->sc_y > 127)
    277   1.1    andrew 			info.ymotion = 127;
    278   1.8   mycroft 		else if (sc->sc_y < -127)
    279   1.3   mycroft 			info.ymotion = -127;
    280   1.1    andrew 		else
    281   1.8   mycroft 			info.ymotion = sc->sc_y;
    282   1.1    andrew 
    283   1.8   mycroft 		/* Reset historical information. */
    284   1.8   mycroft 		sc->sc_x = sc->sc_y = 0;
    285   1.8   mycroft 		sc->sc_status &= ~BUTCHNGMASK;
    286  1.13       cgd 		ndflush(&sc->sc_q, sc->sc_q.c_cc);
    287   1.1    andrew 
    288   1.1    andrew 		splx(s);
    289   1.1    andrew 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    290   1.1    andrew 		break;
    291   1.1    andrew 
    292   1.1    andrew 	default:
    293   1.1    andrew 		error = EINVAL;
    294   1.1    andrew 		break;
    295   1.8   mycroft 	}
    296   1.1    andrew 
    297   1.8   mycroft 	return error;
    298   1.1    andrew }
    299   1.1    andrew 
    300   1.8   mycroft int
    301  1.19       cgd lmsintr(arg)
    302  1.19       cgd 	void *arg;
    303   1.1    andrew {
    304  1.19       cgd 	struct lms_softc *sc = arg;
    305  1.23   thorpej 	bus_chipset_tag_t bc = sc->sc_bc;
    306  1.23   thorpej 	bus_io_handle_t ioh = sc->sc_ioh;
    307   1.8   mycroft 	u_char hi, lo, buttons, changed;
    308   1.8   mycroft 	char dx, dy;
    309   1.8   mycroft 	u_char buffer[5];
    310   1.8   mycroft 
    311   1.8   mycroft 	if ((sc->sc_state & LMS_OPEN) == 0)
    312   1.8   mycroft 		/* Interrupts are not expected. */
    313   1.8   mycroft 		return 0;
    314   1.8   mycroft 
    315  1.23   thorpej 	bus_io_write_1(bc, ioh, LMS_CNTRL, 0xab);
    316  1.23   thorpej 	hi = bus_io_read_1(bc, ioh, LMS_DATA);
    317  1.23   thorpej 	bus_io_write_1(bc, ioh, LMS_CNTRL, 0x90);
    318  1.23   thorpej 	lo = bus_io_read_1(bc, ioh, LMS_DATA);
    319   1.8   mycroft 	dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
    320   1.8   mycroft 	/* Bounding at -127 avoids a bug in XFree86. */
    321   1.3   mycroft 	dx = (dx == -128) ? -127 : dx;
    322   1.3   mycroft 
    323  1.23   thorpej 	bus_io_write_1(bc, ioh, LMS_CNTRL, 0xf0);
    324  1.23   thorpej 	hi = bus_io_read_1(bc, ioh, LMS_DATA);
    325  1.23   thorpej 	bus_io_write_1(bc, ioh, LMS_CNTRL, 0xd0);
    326  1.23   thorpej 	lo = bus_io_read_1(bc, ioh, LMS_DATA);
    327   1.8   mycroft 	dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
    328   1.1    andrew 	dy = (dy == -128) ? 127 : -dy;
    329   1.3   mycroft 
    330  1.23   thorpej 	bus_io_write_1(bc, ioh, LMS_CNTRL, 0);
    331   1.8   mycroft 
    332   1.8   mycroft 	buttons = (~hi >> 5) & 0x07;
    333   1.8   mycroft 	changed = ((buttons ^ sc->sc_status) & 0x07) << 3;
    334   1.8   mycroft 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    335   1.8   mycroft 
    336   1.8   mycroft 	if (dx || dy || changed) {
    337   1.8   mycroft 		/* Update accumulated movements. */
    338   1.8   mycroft 		sc->sc_x += dx;
    339   1.8   mycroft 		sc->sc_y += dy;
    340   1.8   mycroft 
    341   1.8   mycroft 		/* Add this event to the queue. */
    342   1.8   mycroft 		buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
    343   1.8   mycroft 		buffer[1] = dx;
    344   1.8   mycroft 		buffer[2] = dy;
    345   1.8   mycroft 		buffer[3] = buffer[4] = 0;
    346   1.8   mycroft 		(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    347   1.1    andrew 
    348   1.8   mycroft 		if (sc->sc_state & LMS_ASLP) {
    349   1.8   mycroft 			sc->sc_state &= ~LMS_ASLP;
    350   1.5    andrew 			wakeup((caddr_t)sc);
    351   1.1    andrew 		}
    352   1.8   mycroft 		selwakeup(&sc->sc_rsel);
    353   1.5    andrew 	}
    354   1.8   mycroft 
    355  1.12   mycroft 	return -1;
    356   1.1    andrew }
    357   1.1    andrew 
    358   1.8   mycroft int
    359   1.8   mycroft lmsselect(dev, rw, p)
    360   1.8   mycroft 	dev_t dev;
    361   1.8   mycroft 	int rw;
    362   1.8   mycroft 	struct proc *p;
    363   1.1    andrew {
    364  1.22   thorpej 	struct lms_softc *sc = lms_cd.cd_devs[LMSUNIT(dev)];
    365   1.8   mycroft 	int s;
    366   1.8   mycroft 	int ret;
    367   1.1    andrew 
    368   1.1    andrew 	if (rw == FWRITE)
    369   1.8   mycroft 		return 0;
    370   1.1    andrew 
    371   1.1    andrew 	s = spltty();
    372   1.8   mycroft 	if (!sc->sc_q.c_cc) {
    373   1.8   mycroft 		selrecord(p, &sc->sc_rsel);
    374   1.8   mycroft 		ret = 0;
    375   1.8   mycroft 	} else
    376   1.1    andrew 		ret = 1;
    377   1.1    andrew 	splx(s);
    378   1.1    andrew 
    379   1.8   mycroft 	return ret;
    380   1.1    andrew }
    381