Home | History | Annotate | Line # | Download | only in isa
lms.c revision 1.6.2.5
      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.5 1993/09/30 17:33:00 mycroft Exp $
     24  */
     25 
     26 #include "param.h"
     27 #include "kernel.h"
     28 #include "systm.h"
     29 #include "buf.h"
     30 #include "malloc.h"
     31 #include "ioctl.h"
     32 #include "tty.h"
     33 #include "file.h"
     34 #include "select.h"
     35 #include "proc.h"
     36 #include "vnode.h"
     37 #include "sys/device.h"
     38 
     39 #include "i386/isa/isavar.h"
     40 #include "i386/include/mouse.h"
     41 #include "i386/include/pio.h"
     42 
     43 #define LMS_DATA	0       /* Offset for data port, read-only */
     44 #define LMS_SIGN	1       /* Offset for signature port, read-write */
     45 #define LMS_INTR	2       /* Offset for interrupt port, read-only */
     46 #define LMS_CNTRL	2       /* Offset for control port, write-only */
     47 #define LMS_CONFIG	3	/* for configuration port, read-write */
     48 #define	LMS_NPORTS	4
     49 
     50 #define	LMS_CHUNK	128	/* chunk size for read */
     51 #define	LMS_BSIZE	1024	/* buffer size */
     52 
     53 struct lms_softc {		/* Driver status information */
     54 	struct	device sc_dev;
     55 	struct	isadev sc_id;
     56 	struct	intrhand sc_ih;
     57 
     58 	struct	ringbuf {	/* Input queue */
     59 		int rb_count, rb_first, rb_last;
     60 		char rb_data[LMS_BSIZE];
     61 	} sc_q;
     62 	struct	selinfo sc_rsel;
     63 	u_short	sc_iobase;	/* I/O port base */
     64 	u_char	sc_flags;	/* Driver flags */
     65 #define	LMS_NOBLOCK	0x01
     66 	u_char	sc_state;	/* Mouse driver state */
     67 #define LMS_OPEN	0x01	/* Device is open */
     68 #define LMS_ASLP	0x02	/* Waiting for mouse data */
     69 	u_char	sc_status;	/* Mouse button status */
     70 	int	sc_x, sc_y;	/* accumulated motion in the X,Y axis */
     71 };
     72 
     73 static int lmsprobe __P((struct device *, struct cfdata *, void *));
     74 static void lmsforceintr __P((void *));
     75 static void lmsattach __P((struct device *, struct device *, void *));
     76 static int lmsintr __P((void *));
     77 
     78 struct cfdriver lmscd =
     79 { NULL, "lms", lmsprobe, lmsattach, sizeof (struct lms_softc) };
     80 
     81 #define LMSUNIT(dev)	(minor(dev) >> 1)
     82 #define	LMSFLAGS(dev)	(minor(dev) & 0x01)
     83 
     84 static int
     85 lmsprobe(parent, cf, aux)
     86 	struct device *parent;
     87 	struct cfdata *cf;
     88 	void *aux;
     89 {
     90 	struct	isa_attach_args *ia = aux;
     91 	u_short	iobase = ia->ia_iobase;
     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 	/* XXXX isa_discoverintr */
    106 
    107 	/* disable interrupts */
    108 	outb(iobase + LMS_CNTRL, 0x10);
    109 
    110 	ia->ia_iosize = LMS_NPORTS;
    111 	ia->ia_drq = DRQUNK;
    112 	ia->ia_msize = 0;
    113 	return 1;
    114 }
    115 
    116 static void
    117 lmsforceintr(aux)
    118 	void *aux;
    119 {
    120 	struct	isa_attach_args *ia = aux;
    121 
    122 	/* enable interrupts; expect to get one in 1/60 second */
    123 	outb(ia->ia_iobase + LMS_CNTRL, 0);
    124 }
    125 
    126 static void
    127 lmsattach(parent, self, aux)
    128 	struct device *parent, *self;
    129 	void *aux;
    130 {
    131 	struct	lms_softc *sc = (struct lms_softc *)self;
    132 	struct	isa_attach_args *ia = aux;
    133 	u_short	iobase = ia->ia_iobase;
    134 
    135 	/* other initialization done by lmsprobe */
    136 	sc->sc_iobase = iobase;
    137 	sc->sc_state = 0;
    138 
    139 	printf(": Logitech mouse\n");
    140 
    141 	/* XXXX isa_establishintr */
    142 }
    143 
    144 int
    145 lmsopen(dev, flag)
    146 	dev_t dev;
    147 	int flag;
    148 {
    149 	int	unit = LMSUNIT(dev);
    150 	struct	lms_softc *sc;
    151 
    152 	if (unit >= lmscd.cd_ndevs)
    153 		return ENXIO;
    154 	sc = lmscd.cd_devs[unit];
    155 	if (!sc)
    156 		return ENXIO;
    157 
    158 	if (sc->sc_state & LMS_OPEN)
    159 		return EBUSY;
    160 
    161 	sc->sc_state |= LMS_OPEN;
    162 	sc->sc_status = 0;
    163 	sc->sc_x = sc->sc_y = 0;
    164 	sc->sc_q.rb_count = sc->sc_q.rb_first = sc->sc_q.rb_last = 0;
    165 
    166 	/* enable interrupts */
    167 	outb(sc->sc_iobase + LMS_CNTRL, 0);
    168 
    169 	return 0;
    170 }
    171 
    172 int
    173 lmsclose(dev, flag)
    174 	dev_t dev;
    175 	int flag;
    176 {
    177 	int	unit = LMSUNIT(dev);
    178 	struct	lms_softc *sc = lmscd.cd_devs[unit];
    179 
    180 	/* disable interrupts */
    181 	outb(sc->sc_iobase + LMS_CNTRL, 0x10);
    182 
    183 	sc->sc_state &= ~LMS_OPEN;
    184 
    185 	return 0;
    186 }
    187 
    188 int
    189 lmsread(dev, uio, flag)
    190 	dev_t dev;
    191 	struct uio *uio;
    192 	int flag;
    193 {
    194 	int	unit = LMSUNIT(dev);
    195 	struct	lms_softc *sc = lmscd.cd_devs[unit];
    196 	int	s;
    197 	int	error;
    198 	size_t	length;
    199 	u_char	buffer[LMS_CHUNK];
    200 
    201 	/* Block until mouse activity occured */
    202 
    203 	s = spltty();
    204 	while (sc->sc_q.rb_count == 0) {
    205 		if (sc->sc_flags & LMS_NOBLOCK) {
    206 			splx(s);
    207 			return EWOULDBLOCK;
    208 		}
    209 		sc->sc_state |= LMS_ASLP;
    210 		if (error = tsleep((caddr_t)sc, PZERO | PCATCH, "lmsrea", 0)) {
    211 			sc->sc_state &= ~LMS_ASLP;
    212 			splx(s);
    213 			return error;
    214 		}
    215 	}
    216 
    217 	/* Transfer as many chunks as possible */
    218 
    219 	while (sc->sc_q.rb_count > 0 && uio->uio_resid > 0) {
    220 		length = min(sc->sc_q.rb_count, uio->uio_resid);
    221 		if (length > sizeof(buffer))
    222 			length = sizeof(buffer);
    223 
    224 		/* Remove a small chunk from input queue */
    225 
    226 		if (sc->sc_q.rb_first + length >= LMS_BSIZE) {
    227 			size_t	left = LMS_BSIZE - sc->sc_q.rb_first;
    228 			bcopy(&sc->sc_q.rb_data[sc->sc_q.rb_first], buffer,
    229 			      left);
    230 			bcopy(sc->sc_q.rb_data, &buffer[left], length - left);
    231 		} else
    232 			bcopy(&sc->sc_q.rb_data[sc->sc_q.rb_first], buffer,
    233 			      length);
    234 
    235 		sc->sc_q.rb_first = (sc->sc_q.rb_first + length) % LMS_BSIZE;
    236 		sc->sc_q.rb_count -= length;
    237 
    238 		/* Copy data to user process */
    239 
    240 		if (error = uiomove(buffer, length, uio))
    241 			break;
    242 	}
    243 
    244 	/* reset counters */
    245 	sc->sc_x = sc->sc_y = 0;
    246 
    247 	splx(s);
    248 	return error;
    249 }
    250 
    251 int
    252 lmsioctl(dev, cmd, addr, flag)
    253 	dev_t dev;
    254 	int cmd;
    255 	caddr_t addr;
    256 	int flag;
    257 {
    258 	int	unit = LMSUNIT(dev);
    259 	struct	lms_softc *sc = lmscd.cd_devs[unit];
    260 	struct	mouseinfo info;
    261 	int	s;
    262 	int	error;
    263 
    264 	switch (cmd) {
    265 	    case MOUSEIOCREAD:
    266 		s = spltty();
    267 
    268 		info.status = sc->sc_status;
    269 		if (sc->sc_x || sc->sc_y)
    270 			info.status |= MOVEMENT;
    271 
    272 		if (sc->sc_x > 127)
    273 			info.xmotion = 127;
    274 		else if (sc->sc_x < -127)
    275 			/* bounding at -127 avoids a bug in XFree86 */
    276 			info.xmotion = -127;
    277 		else
    278 			info.xmotion = sc->sc_x;
    279 
    280 		if (sc->sc_y > 127)
    281 			info.ymotion = 127;
    282 		else if (sc->sc_y < -127)
    283 			info.ymotion = -127;
    284 		else
    285 			info.ymotion = sc->sc_y;
    286 
    287 		sc->sc_x = 0;
    288 		sc->sc_y = 0;
    289 		sc->sc_status &= ~BUTCHNGMASK;
    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 
    312 	if ((sc->sc_state & LMS_OPEN) == 0)
    313 		/* interrupts not expected */
    314 		return 0;
    315 
    316 	outb(iobase + LMS_CNTRL, 0xab);
    317 	hi = inb(iobase + LMS_DATA);
    318 	outb(iobase + LMS_CNTRL, 0x90);
    319 	lo = inb(iobase + LMS_DATA);
    320 	dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
    321 	/* bounding at -127 avoids a bug in XFree86 */
    322 	dx = (dx == -128) ? -127 : dx;
    323 
    324 	outb(iobase + LMS_CNTRL, 0xf0);
    325 	hi = inb(iobase + LMS_DATA);
    326 	outb(iobase + LMS_CNTRL, 0xd0);
    327 	lo = inb(iobase + LMS_DATA);
    328 	dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
    329 	dy = (dy == -128) ? 127 : -dy;
    330 
    331 	outb(iobase + LMS_CNTRL, 0);
    332 
    333 	buttons = (~hi >> 5) & 0x07;
    334 	changed = ((buttons ^ sc->sc_status) & 0x07) << 3;
    335 	sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    336 
    337 	if (dx || dy || changed) {
    338 		int	last = sc->sc_q.rb_last;
    339 		char	*cp = &sc->sc_q.rb_data[last];
    340 		int	count = sc->sc_q.rb_count;
    341 
    342 		/* Update accumulated movements */
    343 		sc->sc_x += dx;
    344 		sc->sc_y += dy;
    345 
    346 		if ((count += 5) > LMS_BSIZE)
    347 			return 1;
    348 		sc->sc_q.rb_count = count;
    349 
    350 #define	next() \
    351 		if (++last >= LMS_BSIZE) {	\
    352 			last = 0;		\
    353 			cp = sc->sc_q.rb_data;	\
    354 		} else				\
    355 			cp++;
    356 		*cp = 0x80 | (buttons ^ BUTSTATMASK);
    357 		next();
    358 		*cp = dx;
    359 		next();
    360 		*cp = dy;
    361 		next();
    362 		*cp = 0;
    363 		next();
    364 		*cp = 0;
    365 		next();
    366 		sc->sc_q.rb_last = last;
    367 
    368 		if (sc->sc_state & LMS_ASLP) {
    369 			sc->sc_state &= ~LMS_ASLP;
    370 			wakeup((caddr_t)sc);
    371 		}
    372 		selwakeup(&sc->sc_rsel);
    373 	}
    374 
    375 	return 1;
    376 }
    377 
    378 int
    379 lmsselect(dev, rw, p)
    380 	dev_t dev;
    381 	int rw;
    382 	struct proc *p;
    383 {
    384 	int	unit = LMSUNIT(dev);
    385 	struct	lms_softc *sc = lmscd.cd_devs[unit];
    386 	int	s;
    387 	int	ret;
    388 
    389 	if (rw == FWRITE)
    390 		return 0;
    391 
    392 	s = spltty();
    393 	if (sc->sc_q.rb_count)
    394 		ret = 1;
    395 	else {
    396 		selrecord(p, &sc->sc_rsel);
    397 		ret = 0;
    398 	}
    399 	splx(s);
    400 
    401 	return ret;
    402 }
    403