Home | History | Annotate | Line # | Download | only in dev
opms.c revision 1.1.4.1
      1 /*	$NetBSD: opms.c,v 1.1.4.1 2001/09/09 02:39:42 thorpej Exp $	*/
      2 /*	$OpenBSD: pccons.c,v 1.22 1999/01/30 22:39:37 imp Exp $	*/
      3 /*	NetBSD: pms.c,v 1.21 1995/04/18 02:25:18 mycroft Exp	*/
      4 
      5 /*-
      6  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.  All rights reserved.
      7  * Copyright (c) 1990 The Regents of the University of California.
      8  * All rights reserved.
      9  *
     10  * This code is derived from software contributed to Berkeley by
     11  * William Jolitz and Don Ahn.
     12  *
     13  * Copyright (c) 1994 Charles M. Hannum.
     14  * Copyright (c) 1992, 1993 Erik Forsberg.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *	@(#)pccons.c	5.11 (Berkeley) 5/21/91
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/vnode.h>
     50 #include <sys/poll.h>
     51 #include <sys/tty.h>
     52 #include <sys/device.h>
     53 
     54 #include <machine/bus.h>
     55 #include <machine/kbdreg.h>
     56 #include <machine/mouse.h>
     57 
     58 #include <arc/dev/pcconsvar.h>
     59 #include <arc/dev/opmsvar.h>
     60 
     61 #define	PMSUNIT(dev)	(minor(dev))
     62 
     63 /* status bits */
     64 #define	PMS_OBUF_FULL	0x01
     65 #define	PMS_IBUF_FULL	0x02
     66 
     67 /* controller commands */
     68 #define	PMS_INT_ENABLE	0x47	/* enable controller interrupts */
     69 #define	PMS_INT_DISABLE	0x65	/* disable controller interrupts */
     70 #define	PMS_AUX_ENABLE	0xa7	/* enable auxiliary port */
     71 #define	PMS_AUX_DISABLE	0xa8	/* disable auxiliary port */
     72 #define	PMS_MAGIC_1	0xa9	/* XXX */
     73 
     74 #define	PMS_8042_CMD	0x65
     75 
     76 /* mouse commands */
     77 #define	PMS_SET_SCALE11	0xe6	/* set scaling 1:1 */
     78 #define	PMS_SET_SCALE21 0xe7	/* set scaling 2:1 */
     79 #define	PMS_SET_RES	0xe8	/* set resolution */
     80 #define	PMS_GET_SCALE	0xe9	/* get scaling factor */
     81 #define	PMS_SET_STREAM	0xea	/* set streaming mode */
     82 #define	PMS_SET_SAMPLE	0xf3	/* set sampling rate */
     83 #define	PMS_DEV_ENABLE	0xf4	/* mouse on */
     84 #define	PMS_DEV_DISABLE	0xf5	/* mouse off */
     85 #define	PMS_RESET	0xff	/* reset */
     86 
     87 #define	PMS_CHUNK	128	/* chunk size for read */
     88 #define	PMS_BSIZE	1020	/* buffer size */
     89 
     90 #define	FLUSHQ(q) { if((q)->c_cc) ndflush(q, (q)->c_cc); }
     91 
     92 extern struct cfdriver opms_cd;
     93 
     94 cdev_decl(opms);
     95 
     96 static __inline void pms_dev_cmd __P((u_char));
     97 static __inline void pms_aux_cmd __P((u_char));
     98 static __inline void pms_pit_cmd __P((u_char));
     99 
    100 static __inline void
    101 pms_dev_cmd(value)
    102 	u_char value;
    103 {
    104 	kbd_flush_input();
    105 	kbd_cmd_write_1(0xd4);
    106 	kbd_flush_input();
    107 	kbd_data_write_1(value);
    108 }
    109 
    110 static __inline void
    111 pms_aux_cmd(value)
    112 	u_char value;
    113 {
    114 	kbd_flush_input();
    115 	kbd_cmd_write_1(value);
    116 }
    117 
    118 static __inline void
    119 pms_pit_cmd(value)
    120 	u_char value;
    121 {
    122 	kbd_flush_input();
    123 	kbd_cmd_write_1(0x60);
    124 	kbd_flush_input();
    125 	kbd_data_write_1(value);
    126 }
    127 
    128 int opms_common_match(kbd_iot, config)
    129 	bus_space_tag_t kbd_iot;
    130 	struct pccons_config *config;
    131 {
    132 	u_char x;
    133 
    134 	kbd_context_init(kbd_iot, config);
    135 
    136 	pms_dev_cmd(KBC_RESET);
    137 	pms_aux_cmd(PMS_MAGIC_1);
    138 	delay(10000);
    139 	x = kbd_data_read_1();
    140 	pms_pit_cmd(PMS_INT_DISABLE);
    141 	if (x & 0x04)
    142 		return 0;
    143 
    144 	return 1;
    145 }
    146 
    147 void
    148 opms_common_attach(sc, opms_iot, config)
    149 	struct opms_softc *sc;
    150 	bus_space_tag_t opms_iot;
    151 	struct pccons_config *config;
    152 {
    153 	kbd_context_init(opms_iot, config);
    154 
    155 	/* Other initialization was done by opmsprobe. */
    156 	sc->sc_state = 0;
    157 }
    158 
    159 int
    160 opmsopen(dev, flag)
    161 	dev_t dev;
    162 	int flag;
    163 {
    164 	int unit = PMSUNIT(dev);
    165 	struct opms_softc *sc;
    166 
    167 	if (unit >= opms_cd.cd_ndevs)
    168 		return ENXIO;
    169 	sc = opms_cd.cd_devs[unit];
    170 	if (!sc)
    171 		return ENXIO;
    172 
    173 	if (sc->sc_state & PMS_OPEN)
    174 		return EBUSY;
    175 
    176 	if (clalloc(&sc->sc_q, PMS_BSIZE, 0) == -1)
    177 		return ENOMEM;
    178 
    179 	sc->sc_state |= PMS_OPEN;
    180 	sc->sc_status = 0;
    181 	sc->sc_x = sc->sc_y = 0;
    182 
    183 	/* Enable interrupts. */
    184 	pms_dev_cmd(PMS_DEV_ENABLE);
    185 	pms_aux_cmd(PMS_AUX_ENABLE);
    186 	pms_dev_cmd(PMS_SET_RES);
    187 	pms_dev_cmd(3);		/* 8 counts/mm */
    188 	pms_dev_cmd(PMS_SET_SCALE21);
    189 #if 0
    190 	pms_dev_cmd(PMS_SET_SAMPLE);
    191 	pms_dev_cmd(100);	/* 100 samples/sec */
    192 	pms_dev_cmd(PMS_SET_STREAM);
    193 #endif
    194 	pms_pit_cmd(PMS_INT_ENABLE);
    195 
    196 	return 0;
    197 }
    198 
    199 int
    200 opmsclose(dev, flag)
    201 	dev_t dev;
    202 	int flag;
    203 {
    204 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    205 
    206 	/* Disable interrupts. */
    207 	pms_dev_cmd(PMS_DEV_DISABLE);
    208 	pms_pit_cmd(PMS_INT_DISABLE);
    209 	pms_aux_cmd(PMS_AUX_DISABLE);
    210 
    211 	sc->sc_state &= ~PMS_OPEN;
    212 
    213 	clfree(&sc->sc_q);
    214 
    215 	return 0;
    216 }
    217 
    218 int
    219 opmsread(dev, uio, flag)
    220 	dev_t dev;
    221 	struct uio *uio;
    222 	int flag;
    223 {
    224 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    225 	int s;
    226 	int error = 0;
    227 	size_t length;
    228 	u_char buffer[PMS_CHUNK];
    229 
    230 	/* Block until mouse activity occured. */
    231 
    232 	s = spltty();
    233 	while (sc->sc_q.c_cc == 0) {
    234 		if (flag & IO_NDELAY) {
    235 			splx(s);
    236 			return EWOULDBLOCK;
    237 		}
    238 		sc->sc_state |= PMS_ASLP;
    239 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "pmsrea", 0);
    240 		if (error) {
    241 			sc->sc_state &= ~PMS_ASLP;
    242 			splx(s);
    243 			return error;
    244 		}
    245 	}
    246 	splx(s);
    247 
    248 	/* Transfer as many chunks as possible. */
    249 
    250 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    251 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    252 		if (length > sizeof(buffer))
    253 			length = sizeof(buffer);
    254 
    255 		/* Remove a small chunk from the input queue. */
    256 		(void) q_to_b(&sc->sc_q, buffer, length);
    257 
    258 		/* Copy the data to the user process. */
    259 		error = uiomove(buffer, length, uio);
    260 		if (error)
    261 			break;
    262 	}
    263 
    264 	return error;
    265 }
    266 
    267 int
    268 opmsioctl(dev, cmd, addr, flag)
    269 	dev_t dev;
    270 	u_long cmd;
    271 	caddr_t addr;
    272 	int flag;
    273 {
    274 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    275 	struct mouseinfo info;
    276 	int s;
    277 	int error;
    278 
    279 	switch (cmd) {
    280 	case MOUSEIOCREAD:
    281 		s = spltty();
    282 
    283 		info.status = sc->sc_status;
    284 		if (sc->sc_x || sc->sc_y)
    285 			info.status |= MOVEMENT;
    286 
    287 		if (sc->sc_x > 127)
    288 			info.xmotion = 127;
    289 		else if (sc->sc_x < -127)
    290 			/* Bounding at -127 avoids a bug in XFree86. */
    291 			info.xmotion = -127;
    292 		else
    293 			info.xmotion = sc->sc_x;
    294 
    295 		if (sc->sc_y > 127)
    296 			info.ymotion = 127;
    297 		else if (sc->sc_y < -127)
    298 			info.ymotion = -127;
    299 		else
    300 			info.ymotion = sc->sc_y;
    301 
    302 		/* Reset historical information. */
    303 		sc->sc_x = sc->sc_y = 0;
    304 		sc->sc_status &= ~BUTCHNGMASK;
    305 		ndflush(&sc->sc_q, sc->sc_q.c_cc);
    306 
    307 		splx(s);
    308 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    309 		break;
    310 	default:
    311 		error = EINVAL;
    312 		break;
    313 	}
    314 
    315 	return error;
    316 }
    317 
    318 /* Masks for the first byte of a packet */
    319 #define PS2LBUTMASK 0x01
    320 #define PS2RBUTMASK 0x02
    321 #define PS2MBUTMASK 0x04
    322 
    323 int
    324 opmsintr(arg)
    325 	void *arg;
    326 {
    327 	struct opms_softc *sc = arg;
    328 	static int state = 0;
    329 	static u_char buttons;
    330 	u_char changed;
    331 	static char dx, dy;
    332 	u_char buffer[5];
    333 
    334 	if ((sc->sc_state & PMS_OPEN) == 0) {
    335 		/* Interrupts are not expected.  Discard the byte. */
    336 		kbd_flush_input();
    337 		return 0;
    338 	}
    339 
    340 	switch (state) {
    341 
    342 	case 0:
    343 		buttons = kbd_data_read_1();
    344 		if ((buttons & 0xc0) == 0)
    345 			++state;
    346 		break;
    347 
    348 	case 1:
    349 		dx = kbd_data_read_1();
    350 		/* Bounding at -127 avoids a bug in XFree86. */
    351 		dx = (dx == -128) ? -127 : dx;
    352 		++state;
    353 		break;
    354 
    355 	case 2:
    356 		dy = kbd_data_read_1();
    357 		dy = (dy == -128) ? -127 : dy;
    358 		state = 0;
    359 
    360 		buttons = ((buttons & PS2LBUTMASK) << 2) |
    361 			  ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
    362 		changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
    363 		sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    364 
    365 		if (dx || dy || changed) {
    366 			/* Update accumulated movements. */
    367 			sc->sc_x += dx;
    368 			sc->sc_y += dy;
    369 
    370 			/* Add this event to the queue. */
    371 			buffer[0] = 0x80 | (buttons & BUTSTATMASK);
    372 			if(dx < 0)
    373 				buffer[0] |= 0x10;
    374 			buffer[1] = dx & 0x7f;
    375 			if(dy < 0)
    376 				buffer[0] |= 0x20;
    377 			buffer[2] = dy & 0x7f;
    378 			buffer[3] = buffer[4] = 0;
    379 			(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    380 
    381 			if (sc->sc_state & PMS_ASLP) {
    382 				sc->sc_state &= ~PMS_ASLP;
    383 				wakeup((caddr_t)sc);
    384 			}
    385 			selnotify(&sc->sc_rsel, 0);
    386 		}
    387 
    388 		break;
    389 	}
    390 	return -1;
    391 }
    392 
    393 int
    394 opmspoll(dev, events, p)
    395 	dev_t dev;
    396 	int events;
    397 	struct proc *p;
    398 {
    399 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    400 	int revents = 0;
    401 	int s = spltty();
    402 
    403 	if (events & (POLLIN | POLLRDNORM)) {
    404 		if (sc->sc_q.c_cc > 0)
    405 			revents |= events & (POLLIN | POLLRDNORM);
    406 		else
    407 			selrecord(p, &sc->sc_rsel);
    408 	}
    409 
    410 	splx(s);
    411 	return (revents);
    412 }
    413 
    414 static void
    415 filt_opmsrdetach(struct knote *kn)
    416 {
    417 	struct opms_softc *sc = (void *) kn->kn_hook;
    418 	int s;
    419 
    420 	s = spltty();
    421 	SLIST_REMOVE(&sc->sc_rsel.si_klist, kn, knote, kn_selnext);
    422 	splx(s);
    423 }
    424 
    425 static int
    426 filt_opmsread(struct knote *kn, long hint)
    427 {
    428 	struct opms_softc *sc = (void *) kn->kn_hook;
    429 
    430 	kn->kn_data = sc->sc_q.c_cc;
    431 	return (kn->kn_data > 0);
    432 }
    433 
    434 static const struct filterops opmsread_filtops =
    435 	{ 1, NULL, filt_opmsrdetach, filt_opmsread };
    436 
    437 int
    438 opmskqfilter(dev_t dev, struct knote *kn)
    439 {
    440 	struct opms_softc *sc = opms_cd.cd_devs[LMSUNIT(dev)];
    441 	struct klist *klist;
    442 	int s;
    443 
    444 	switch (kn->kn_filter) {
    445 	case EVFILT_READ:
    446 		klist = &sc->sc_rsel.si_klist;
    447 		kn->kn_fop = &opmsread_filtops;
    448 		break;
    449 
    450 	default:
    451 		return (1);
    452 	}
    453 
    454 	kn->kn_hook = (void *) sc;
    455 
    456 	s = spltty();
    457 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    458 	splx(s);
    459 
    460 	return (0);
    461 }
    462