Home | History | Annotate | Line # | Download | only in isa
lms.c revision 1.6.2.13
      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.13 1993/10/17 05:33:34 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 	if (ia->ia_irq == IRQUNK) {
    106 		ia->ia_irq = isa_discoverintr(lmsforceintr, aux);
    107 		if (ia->ia_irq == IRQNONE)
    108 			return 0;
    109 	}
    110 
    111 	/* disable interrupts */
    112 	outb(iobase + LMS_CNTRL, 0x10);
    113 
    114 	ia->ia_iosize = LMS_NPORTS;
    115 	ia->ia_drq = DRQUNK;
    116 	ia->ia_msize = 0;
    117 	return 1;
    118 }
    119 
    120 static void
    121 lmsforceintr(aux)
    122 	void *aux;
    123 {
    124 	struct	isa_attach_args *ia = aux;
    125 
    126 	/* enable interrupts; expect to get one in 1/60 second */
    127 	outb(ia->ia_iobase + LMS_CNTRL, 0);
    128 }
    129 
    130 static void
    131 lmsattach(parent, self, aux)
    132 	struct device *parent, *self;
    133 	void *aux;
    134 {
    135 	struct	lms_softc *sc = (struct lms_softc *)self;
    136 	struct	isa_attach_args *ia = aux;
    137 	u_short	iobase = ia->ia_iobase;
    138 
    139 	/* other initialization done by lmsprobe */
    140 	sc->sc_iobase = iobase;
    141 	sc->sc_state = 0;
    142 
    143 	printf(": Logitech mouse\n");
    144 	isa_establish(&sc->sc_id, &sc->sc_dev);
    145 
    146 	sc->sc_ih.ih_fun = lmsintr;
    147 	sc->sc_ih.ih_arg = sc;
    148 	intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
    149 }
    150 
    151 int
    152 lmsopen(dev, flag)
    153 	dev_t dev;
    154 	int flag;
    155 {
    156 	int	unit = LMSUNIT(dev);
    157 	struct	lms_softc *sc;
    158 
    159 	if (unit >= lmscd.cd_ndevs)
    160 		return ENXIO;
    161 	sc = lmscd.cd_devs[unit];
    162 	if (!sc)
    163 		return ENXIO;
    164 
    165 	if (sc->sc_state & LMS_OPEN)
    166 		return EBUSY;
    167 
    168 	if (clalloc(&sc->sc_q, LMS_BSIZE, 0) == -1)
    169 		return ENOMEM;
    170 
    171 	sc->sc_state |= LMS_OPEN;
    172 	sc->sc_status = 0;
    173 	sc->sc_x = sc->sc_y = 0;
    174 
    175 	/* enable interrupts */
    176 	outb(sc->sc_iobase + LMS_CNTRL, 0);
    177 
    178 	return 0;
    179 }
    180 
    181 int
    182 lmsclose(dev, flag)
    183 	dev_t dev;
    184 	int flag;
    185 {
    186 	int	unit = LMSUNIT(dev);
    187 	struct	lms_softc *sc = lmscd.cd_devs[unit];
    188 
    189 	/* disable interrupts */
    190 	outb(sc->sc_iobase + LMS_CNTRL, 0x10);
    191 
    192 	sc->sc_state &= ~LMS_OPEN;
    193 
    194 	clfree(&sc->sc_q);
    195 
    196 	return 0;
    197 }
    198 
    199 int
    200 lmsread(dev, uio, flag)
    201 	dev_t dev;
    202 	struct uio *uio;
    203 	int flag;
    204 {
    205 	int	unit = LMSUNIT(dev);
    206 	struct	lms_softc *sc = lmscd.cd_devs[unit];
    207 	int	s;
    208 	int	error;
    209 	size_t	length;
    210 	u_char	buffer[LMS_CHUNK];
    211 
    212 	/* Block until mouse activity occured */
    213 
    214 	s = spltty();
    215 	while (sc->sc_q.c_cc == 0) {
    216 		if (flag & IO_NDELAY) {
    217 			splx(s);
    218 			return EWOULDBLOCK;
    219 		}
    220 		sc->sc_state |= LMS_ASLP;
    221 		if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0)) {
    222 			sc->sc_state &= ~LMS_ASLP;
    223 			splx(s);
    224 			return error;
    225 		}
    226 	}
    227 	splx(s);
    228 
    229 	/* Transfer as many chunks as possible */
    230 
    231 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    232 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    233 		if (length > sizeof(buffer))
    234 			length = sizeof(buffer);
    235 
    236 		/* remove a small chunk from input queue */
    237 		(void) q_to_b(&sc->sc_q, buffer, length);
    238 
    239 		/* copy data to user process */
    240 		if (error = uiomove(buffer, length, uio))
    241 			break;
    242 	}
    243 
    244 	return error;
    245 }
    246 
    247 int
    248 lmsioctl(dev, cmd, addr, flag)
    249 	dev_t dev;
    250 	int cmd;
    251 	caddr_t addr;
    252 	int flag;
    253 {
    254 	int	unit = LMSUNIT(dev);
    255 	struct	lms_softc *sc = lmscd.cd_devs[unit];
    256 	struct	mouseinfo info;
    257 	int	s;
    258 	int	error;
    259 
    260 	switch (cmd) {
    261 	    case MOUSEIOCREAD:
    262 		s = spltty();
    263 
    264 		info.status = sc->sc_status;
    265 		if (sc->sc_x || sc->sc_y)
    266 			info.status |= MOVEMENT;
    267 
    268 		if (sc->sc_x > 127)
    269 			info.xmotion = 127;
    270 		else if (sc->sc_x < -127)
    271 			/* bounding at -127 avoids a bug in XFree86 */
    272 			info.xmotion = -127;
    273 		else
    274 			info.xmotion = sc->sc_x;
    275 
    276 		if (sc->sc_y > 127)
    277 			info.ymotion = 127;
    278 		else if (sc->sc_y < -127)
    279 			info.ymotion = -127;
    280 		else
    281 			info.ymotion = sc->sc_y;
    282 
    283 		/* reset historical information */
    284 		sc->sc_x = 0;
    285 		sc->sc_y = 0;
    286 		sc->sc_status &= ~BUTCHNGMASK;
    287 		flushq(&sc->sc_q);
    288 
    289 		splx(s);
    290 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    291 		break;
    292 
    293 	    default:
    294 		error = EINVAL;
    295 		break;
    296 	}
    297 
    298 	return error;
    299 }
    300 
    301 int
    302 lmsintr(arg)
    303 	void *arg;
    304 {
    305 	struct	lms_softc *sc = (struct lms_softc *)arg;
    306 	u_short	iobase = sc->sc_iobase;
    307 	u_char	hi, lo, buttons, changed;
    308 	char	dx, dy;
    309 	u_char	buffer[5];
    310 
    311 	if ((sc->sc_state & LMS_OPEN) == 0)
    312 		/* interrupts not expected */
    313 		return 0;
    314 
    315 	outb(iobase + LMS_CNTRL, 0xab);
    316 	hi = inb(iobase + LMS_DATA);
    317 	outb(iobase + LMS_CNTRL, 0x90);
    318 	lo = inb(iobase + LMS_DATA);
    319 	dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
    320 	/* bounding at -127 avoids a bug in XFree86 */
    321 	dx = (dx == -128) ? -127 : dx;
    322 
    323 	outb(iobase + LMS_CNTRL, 0xf0);
    324 	hi = inb(iobase + LMS_DATA);
    325 	outb(iobase + LMS_CNTRL, 0xd0);
    326 	lo = inb(iobase + LMS_DATA);
    327 	dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
    328 	dy = (dy == -128) ? 127 : -dy;
    329 
    330 	outb(iobase + LMS_CNTRL, 0);
    331 
    332 	buttons = (~hi >> 5) & 0x07;
    333 	changed = ((buttons ^ sc->sc_status) & 0x07) << 3;
    334 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    335 
    336 	if (dx || dy || changed) {
    337 		/* update accumulated movements */
    338 		sc->sc_x += dx;
    339 		sc->sc_y += dy;
    340 
    341 		buffer[0] = 0x80 | (buttons ^ BUTSTATMASK);
    342 		buffer[1] = dx;
    343 		buffer[2] = dy;
    344 		buffer[3] = buffer[4] = 0;
    345 		(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    346 
    347 		if (sc->sc_state & LMS_ASLP) {
    348 			sc->sc_state &= ~LMS_ASLP;
    349 			wakeup((caddr_t)sc);
    350 		}
    351 		selwakeup(&sc->sc_rsel);
    352 	}
    353 
    354 	return 1;
    355 }
    356 
    357 int
    358 lmsselect(dev, rw, p)
    359 	dev_t dev;
    360 	int rw;
    361 	struct proc *p;
    362 {
    363 	int	unit = LMSUNIT(dev);
    364 	struct	lms_softc *sc = lmscd.cd_devs[unit];
    365 	int	s;
    366 	int	ret;
    367 
    368 	if (rw == FWRITE)
    369 		return 0;
    370 
    371 	s = spltty();
    372 	if (sc->sc_q.c_cc)
    373 		ret = 1;
    374 	else {
    375 		selrecord(p, &sc->sc_rsel);
    376 		ret = 0;
    377 	}
    378 	splx(s);
    379 
    380 	return ret;
    381 }
    382