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