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