Home | History | Annotate | Line # | Download | only in isa
mms.c revision 1.13
      1  1.13      cgd /*	$NetBSD: mms.c,v 1.13 1994/10/27 04:18:00 cgd Exp $	*/
      2  1.13      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.8  mycroft #include <machine/pio.h>
     41   1.7  mycroft #include <machine/mouse.h>
     42   1.1   andrew 
     43   1.9  mycroft #include <i386/isa/isavar.h>
     44   1.1   andrew 
     45   1.8  mycroft #define	MMS_ADDR	0	/* offset for register select */
     46   1.8  mycroft #define	MMS_DATA	1	/* offset for InPort data */
     47   1.8  mycroft #define	MMS_IDENT	2	/* offset for identification register */
     48   1.8  mycroft #define	MMS_NPORTS	4
     49   1.8  mycroft 
     50   1.8  mycroft #define	MMS_CHUNK	128	/* chunk size for read */
     51   1.8  mycroft #define	MMS_BSIZE	1020	/* buffer size */
     52   1.8  mycroft 
     53   1.8  mycroft struct mms_softc {		/* driver status information */
     54   1.8  mycroft 	struct device sc_dev;
     55  1.11  mycroft 	struct intrhand sc_ih;
     56   1.8  mycroft 
     57   1.8  mycroft 	struct clist sc_q;
     58   1.8  mycroft 	struct selinfo sc_rsel;
     59   1.8  mycroft 	u_short sc_iobase;	/* I/O port base */
     60   1.8  mycroft 	u_char sc_state;	/* mouse driver state */
     61   1.8  mycroft #define	MMS_OPEN	0x01	/* device is open */
     62   1.8  mycroft #define	MMS_ASLP	0x02	/* waiting for mouse data */
     63   1.8  mycroft 	u_char sc_status;	/* mouse button status */
     64   1.8  mycroft 	int sc_x, sc_y;		/* accumulated motion in the X,Y axis */
     65   1.9  mycroft };
     66   1.1   andrew 
     67   1.9  mycroft int mmsprobe();
     68   1.9  mycroft void mmsattach();
     69  1.11  mycroft int mmsintr __P((struct mms_softc *));
     70   1.1   andrew 
     71   1.9  mycroft struct cfdriver mmscd = {
     72   1.9  mycroft 	NULL, "mms", mmsprobe, mmsattach, DV_TTY, sizeof(struct mms_softc)
     73   1.9  mycroft };
     74   1.1   andrew 
     75   1.8  mycroft #define	MMSUNIT(dev)	(minor(dev))
     76   1.8  mycroft 
     77   1.8  mycroft int
     78   1.9  mycroft mmsprobe(parent, self, aux)
     79   1.9  mycroft 	struct device *parent, *self;
     80   1.9  mycroft 	void *aux;
     81   1.1   andrew {
     82   1.9  mycroft 	struct isa_attach_args *ia = aux;
     83   1.9  mycroft 	u_short iobase = ia->ia_iobase;
     84   1.1   andrew 
     85   1.1   andrew 	/* Read identification register to see if present */
     86   1.8  mycroft 	if (inb(iobase + MMS_IDENT) != 0xde)
     87   1.8  mycroft 		return 0;
     88   1.1   andrew 
     89   1.8  mycroft 	/* Seems it was there; reset. */
     90   1.8  mycroft 	outb(iobase + MMS_ADDR, 0x87);
     91   1.1   andrew 
     92   1.9  mycroft 	ia->ia_iosize = MMS_NPORTS;
     93   1.9  mycroft 	ia->ia_msize = 0;
     94   1.9  mycroft 	return 1;
     95   1.1   andrew }
     96   1.1   andrew 
     97   1.9  mycroft void
     98   1.9  mycroft mmsattach(parent, self, aux)
     99   1.9  mycroft 	struct device *parent, *self;
    100   1.9  mycroft 	void *aux;
    101   1.1   andrew {
    102   1.9  mycroft 	struct mms_softc *sc = (void *)self;
    103   1.9  mycroft 	struct isa_attach_args *ia = aux;
    104   1.9  mycroft 	u_short iobase = ia->ia_iobase;
    105  1.10  mycroft 
    106  1.10  mycroft 	printf("\n");
    107   1.1   andrew 
    108   1.8  mycroft 	/* Other initialization was done by mmsprobe. */
    109   1.8  mycroft 	sc->sc_iobase = iobase;
    110   1.8  mycroft 	sc->sc_state = 0;
    111  1.11  mycroft 
    112  1.11  mycroft 	sc->sc_ih.ih_fun = mmsintr;
    113  1.11  mycroft 	sc->sc_ih.ih_arg = sc;
    114  1.11  mycroft 	sc->sc_ih.ih_level = IPL_NONE;
    115  1.11  mycroft 	intr_establish(ia->ia_irq, &sc->sc_ih);
    116   1.1   andrew }
    117   1.1   andrew 
    118   1.8  mycroft int
    119   1.8  mycroft mmsopen(dev, flag)
    120   1.8  mycroft 	dev_t dev;
    121   1.8  mycroft 	int flag;
    122   1.1   andrew {
    123   1.2   andrew 	int unit = MMSUNIT(dev);
    124   1.2   andrew 	struct mms_softc *sc;
    125   1.1   andrew 
    126   1.9  mycroft 	if (unit >= mmscd.cd_ndevs)
    127   1.8  mycroft 		return ENXIO;
    128   1.9  mycroft 	sc = mmscd.cd_devs[unit];
    129   1.9  mycroft 	if (!sc)
    130   1.8  mycroft 		return ENXIO;
    131   1.1   andrew 
    132   1.8  mycroft 	if (sc->sc_state & MMS_OPEN)
    133   1.8  mycroft 		return EBUSY;
    134   1.1   andrew 
    135   1.8  mycroft 	if (clalloc(&sc->sc_q, MMS_BSIZE, 0) == -1)
    136   1.8  mycroft 		return ENOMEM;
    137   1.1   andrew 
    138   1.8  mycroft 	sc->sc_state |= MMS_OPEN;
    139   1.8  mycroft 	sc->sc_status = 0;
    140   1.8  mycroft 	sc->sc_x = sc->sc_y = 0;
    141   1.1   andrew 
    142   1.8  mycroft 	/* Enable interrupts. */
    143   1.8  mycroft 	outb(sc->sc_iobase + MMS_ADDR, 0x07);
    144   1.8  mycroft 	outb(sc->sc_iobase + MMS_DATA, 0x09);
    145   1.1   andrew 
    146   1.8  mycroft 	return 0;
    147   1.1   andrew }
    148   1.1   andrew 
    149   1.8  mycroft int
    150   1.8  mycroft mmsclose(dev, flag)
    151   1.8  mycroft 	dev_t dev;
    152   1.8  mycroft 	int flag;
    153   1.1   andrew {
    154   1.9  mycroft 	struct mms_softc *sc = mmscd.cd_devs[MMSUNIT(dev)];
    155   1.1   andrew 
    156   1.8  mycroft 	/* Disable interrupts. */
    157   1.8  mycroft 	outb(sc->sc_iobase + MMS_ADDR, 0x87);
    158   1.1   andrew 
    159   1.8  mycroft 	sc->sc_state &= ~MMS_OPEN;
    160   1.1   andrew 
    161   1.8  mycroft 	clfree(&sc->sc_q);
    162   1.1   andrew 
    163   1.8  mycroft 	return 0;
    164   1.1   andrew }
    165   1.1   andrew 
    166   1.8  mycroft int
    167   1.8  mycroft mmsread(dev, uio, flag)
    168   1.8  mycroft 	dev_t dev;
    169   1.8  mycroft 	struct uio *uio;
    170   1.8  mycroft 	int flag;
    171   1.1   andrew {
    172   1.9  mycroft 	struct mms_softc *sc = mmscd.cd_devs[MMSUNIT(dev)];
    173   1.8  mycroft 	int s;
    174   1.8  mycroft 	int error;
    175   1.8  mycroft 	size_t length;
    176   1.8  mycroft 	u_char buffer[MMS_CHUNK];
    177   1.1   andrew 
    178   1.8  mycroft 	/* Block until mouse activity occured. */
    179   1.1   andrew 
    180   1.1   andrew 	s = spltty();
    181   1.8  mycroft 	while (sc->sc_q.c_cc == 0) {
    182   1.8  mycroft 		if (flag & IO_NDELAY) {
    183   1.1   andrew 			splx(s);
    184   1.8  mycroft 			return EWOULDBLOCK;
    185   1.1   andrew 		}
    186   1.8  mycroft 		sc->sc_state |= MMS_ASLP;
    187   1.8  mycroft 		if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "mmsrea", 0)) {
    188   1.8  mycroft 			sc->sc_state &= ~MMS_ASLP;
    189   1.1   andrew 			splx(s);
    190   1.8  mycroft 			return error;
    191   1.1   andrew 		}
    192   1.1   andrew 	}
    193   1.8  mycroft 	splx(s);
    194   1.1   andrew 
    195   1.8  mycroft 	/* Transfer as many chunks as possible. */
    196   1.1   andrew 
    197   1.8  mycroft 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    198   1.8  mycroft 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    199   1.1   andrew 		if (length > sizeof(buffer))
    200   1.1   andrew 			length = sizeof(buffer);
    201   1.1   andrew 
    202   1.8  mycroft 		/* Remove a small chunk from the input queue. */
    203   1.8  mycroft 		(void) q_to_b(&sc->sc_q, buffer, length);
    204   1.1   andrew 
    205   1.8  mycroft 		/* Copy the data to the user process. */
    206   1.8  mycroft 		if (error = uiomove(buffer, length, uio))
    207   1.1   andrew 			break;
    208   1.1   andrew 	}
    209   1.1   andrew 
    210   1.8  mycroft 	return error;
    211   1.1   andrew }
    212   1.1   andrew 
    213   1.8  mycroft int
    214   1.8  mycroft mmsioctl(dev, cmd, addr, flag)
    215   1.8  mycroft 	dev_t dev;
    216   1.8  mycroft 	int cmd;
    217   1.8  mycroft 	caddr_t addr;
    218   1.8  mycroft 	int flag;
    219   1.1   andrew {
    220   1.9  mycroft 	struct mms_softc *sc = mmscd.cd_devs[MMSUNIT(dev)];
    221   1.1   andrew 	struct mouseinfo info;
    222   1.8  mycroft 	int s;
    223   1.8  mycroft 	int error;
    224   1.1   andrew 
    225   1.1   andrew 	switch (cmd) {
    226   1.1   andrew 	case MOUSEIOCREAD:
    227   1.1   andrew 		s = spltty();
    228   1.1   andrew 
    229   1.8  mycroft 		info.status = sc->sc_status;
    230   1.8  mycroft 		if (sc->sc_x || sc->sc_y)
    231   1.1   andrew 			info.status |= MOVEMENT;
    232   1.1   andrew 
    233   1.8  mycroft 		if (sc->sc_x > 127)
    234   1.1   andrew 			info.xmotion = 127;
    235   1.8  mycroft 		else if (sc->sc_x < -127)
    236   1.8  mycroft 			/* Bounding at -127 avoids a bug in XFree86. */
    237   1.8  mycroft 			info.xmotion = -127;
    238   1.1   andrew 		else
    239   1.8  mycroft 			info.xmotion = sc->sc_x;
    240   1.1   andrew 
    241   1.8  mycroft 		if (sc->sc_y > 127)
    242   1.1   andrew 			info.ymotion = 127;
    243   1.8  mycroft 		else if (sc->sc_y < -127)
    244   1.8  mycroft 			info.ymotion = -127;
    245   1.1   andrew 		else
    246   1.8  mycroft 			info.ymotion = sc->sc_y;
    247   1.1   andrew 
    248   1.8  mycroft 		/* Reset historical information. */
    249   1.8  mycroft 		sc->sc_x = sc->sc_y = 0;
    250   1.8  mycroft 		sc->sc_status &= ~BUTCHNGMASK;
    251  1.12      cgd 		ndflush(&sc->sc_q, sc->sc_q.c_cc);
    252   1.1   andrew 
    253   1.1   andrew 		splx(s);
    254   1.1   andrew 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    255   1.1   andrew 		break;
    256   1.1   andrew 
    257   1.1   andrew 	default:
    258   1.1   andrew 		error = EINVAL;
    259   1.1   andrew 		break;
    260   1.8  mycroft 	}
    261   1.1   andrew 
    262   1.8  mycroft 	return error;
    263   1.1   andrew }
    264   1.1   andrew 
    265   1.8  mycroft int
    266  1.11  mycroft mmsintr(sc)
    267  1.11  mycroft 	struct mms_softc *sc;
    268   1.1   andrew {
    269   1.8  mycroft 	u_short iobase = sc->sc_iobase;
    270   1.8  mycroft 	u_char buttons, changed, status;
    271   1.8  mycroft 	char dx, dy;
    272   1.8  mycroft 	u_char buffer[5];
    273   1.8  mycroft 
    274   1.8  mycroft 	if ((sc->sc_state & MMS_OPEN) == 0)
    275   1.8  mycroft 		/* Interrupts are not expected. */
    276   1.8  mycroft 		return 0;
    277   1.8  mycroft 
    278   1.8  mycroft 	/* Freeze InPort registers (disabling interrupts). */
    279   1.8  mycroft 	outb(iobase + MMS_ADDR, 0x07);
    280   1.8  mycroft 	outb(iobase + MMS_DATA, 0x29);
    281   1.1   andrew 
    282   1.8  mycroft 	outb(iobase + MMS_ADDR, 0x00);
    283   1.8  mycroft 	status = inb(iobase + MMS_DATA);
    284   1.1   andrew 
    285   1.1   andrew 	if (status & 0x40) {
    286   1.8  mycroft 		outb(iobase + MMS_ADDR, 1);
    287   1.8  mycroft 		dx = inb(iobase + MMS_DATA);
    288   1.8  mycroft 		dx = (dx == -128) ? -127 : dx;
    289   1.8  mycroft 		outb(iobase + MMS_ADDR, 2);
    290   1.8  mycroft 		dy = inb(iobase + MMS_DATA);
    291   1.1   andrew 		dy = (dy == -128) ? 127 : -dy;
    292   1.8  mycroft 	} else
    293   1.1   andrew 		dx = dy = 0;
    294   1.1   andrew 
    295   1.8  mycroft 	/* Unfreeze InPort registers (reenabling interrupts). */
    296   1.8  mycroft 	outb(iobase + MMS_ADDR, 0x07);
    297   1.8  mycroft 	outb(iobase + MMS_DATA, 0x09);
    298   1.8  mycroft 
    299   1.8  mycroft 	buttons = status & BUTSTATMASK;
    300   1.8  mycroft 	changed = status & BUTCHNGMASK;
    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 & MMS_ASLP) {
    316   1.8  mycroft 			sc->sc_state &= ~MMS_ASLP;
    317   1.5   andrew 			wakeup((caddr_t)sc);
    318   1.1   andrew 		}
    319   1.8  mycroft 		selwakeup(&sc->sc_rsel);
    320   1.8  mycroft 	}
    321   1.8  mycroft 
    322  1.11  mycroft 	return -1;
    323   1.1   andrew }
    324   1.1   andrew 
    325   1.8  mycroft int
    326   1.8  mycroft mmsselect(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.9  mycroft 	struct mms_softc *sc = mmscd.cd_devs[MMSUNIT(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