Home | History | Annotate | Line # | Download | only in dev
opms.c revision 1.1.4.3
      1 /*	$NetBSD: opms.c,v 1.1.4.3 2002/06/16 20:11:33 jdolecek 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 #include <sys/proc.h>
     54 #include <sys/conf.h>
     55 
     56 #include <machine/bus.h>
     57 #include <machine/kbdreg.h>
     58 #include <machine/mouse.h>
     59 
     60 #include <arc/dev/pcconsvar.h>
     61 #include <arc/dev/opmsvar.h>
     62 
     63 #define	PMSUNIT(dev)	(minor(dev))
     64 
     65 /* status bits */
     66 #define	PMS_OBUF_FULL	0x01
     67 #define	PMS_IBUF_FULL	0x02
     68 
     69 /* controller commands */
     70 #define	PMS_INT_ENABLE	0x47	/* enable controller interrupts */
     71 #define	PMS_INT_DISABLE	0x65	/* disable controller interrupts */
     72 #define	PMS_AUX_ENABLE	0xa7	/* enable auxiliary port */
     73 #define	PMS_AUX_DISABLE	0xa8	/* disable auxiliary port */
     74 #define	PMS_MAGIC_1	0xa9	/* XXX */
     75 
     76 #define	PMS_8042_CMD	0x65
     77 
     78 /* mouse commands */
     79 #define	PMS_SET_SCALE11	0xe6	/* set scaling 1:1 */
     80 #define	PMS_SET_SCALE21 0xe7	/* set scaling 2:1 */
     81 #define	PMS_SET_RES	0xe8	/* set resolution */
     82 #define	PMS_GET_SCALE	0xe9	/* get scaling factor */
     83 #define	PMS_SET_STREAM	0xea	/* set streaming mode */
     84 #define	PMS_SET_SAMPLE	0xf3	/* set sampling rate */
     85 #define	PMS_DEV_ENABLE	0xf4	/* mouse on */
     86 #define	PMS_DEV_DISABLE	0xf5	/* mouse off */
     87 #define	PMS_RESET	0xff	/* reset */
     88 
     89 #define	PMS_CHUNK	128	/* chunk size for read */
     90 #define	PMS_BSIZE	1020	/* buffer size */
     91 
     92 #define	FLUSHQ(q) { if((q)->c_cc) ndflush(q, (q)->c_cc); }
     93 
     94 extern struct cfdriver opms_cd;
     95 
     96 cdev_decl(opms);
     97 
     98 #define	LMSUNIT(dev)	(minor(dev))
     99 
    100 static __inline void pms_dev_cmd __P((u_char));
    101 static __inline void pms_aux_cmd __P((u_char));
    102 static __inline void pms_pit_cmd __P((u_char));
    103 
    104 static __inline void
    105 pms_dev_cmd(value)
    106 	u_char value;
    107 {
    108 	kbd_flush_input();
    109 	kbd_cmd_write_1(0xd4);
    110 	kbd_flush_input();
    111 	kbd_data_write_1(value);
    112 }
    113 
    114 static __inline void
    115 pms_aux_cmd(value)
    116 	u_char value;
    117 {
    118 	kbd_flush_input();
    119 	kbd_cmd_write_1(value);
    120 }
    121 
    122 static __inline void
    123 pms_pit_cmd(value)
    124 	u_char value;
    125 {
    126 	kbd_flush_input();
    127 	kbd_cmd_write_1(0x60);
    128 	kbd_flush_input();
    129 	kbd_data_write_1(value);
    130 }
    131 
    132 int opms_common_match(kbd_iot, config)
    133 	bus_space_tag_t kbd_iot;
    134 	struct pccons_config *config;
    135 {
    136 	u_char x;
    137 
    138 	kbd_context_init(kbd_iot, config);
    139 
    140 	pms_dev_cmd(KBC_RESET);
    141 	pms_aux_cmd(PMS_MAGIC_1);
    142 	delay(10000);
    143 	x = kbd_data_read_1();
    144 	pms_pit_cmd(PMS_INT_DISABLE);
    145 	if (x & 0x04)
    146 		return 0;
    147 
    148 	return 1;
    149 }
    150 
    151 void
    152 opms_common_attach(sc, opms_iot, config)
    153 	struct opms_softc *sc;
    154 	bus_space_tag_t opms_iot;
    155 	struct pccons_config *config;
    156 {
    157 	kbd_context_init(opms_iot, config);
    158 
    159 	/* Other initialization was done by opmsprobe. */
    160 	sc->sc_state = 0;
    161 }
    162 
    163 int
    164 opmsopen(dev, flag, mode, p)
    165 	dev_t dev;
    166 	int flag;
    167 	int mode;
    168 	struct proc *p;
    169 {
    170 	int unit = PMSUNIT(dev);
    171 	struct opms_softc *sc;
    172 
    173 	if (unit >= opms_cd.cd_ndevs)
    174 		return ENXIO;
    175 	sc = opms_cd.cd_devs[unit];
    176 	if (!sc)
    177 		return ENXIO;
    178 
    179 	if (sc->sc_state & PMS_OPEN)
    180 		return EBUSY;
    181 
    182 	if (clalloc(&sc->sc_q, PMS_BSIZE, 0) == -1)
    183 		return ENOMEM;
    184 
    185 	sc->sc_state |= PMS_OPEN;
    186 	sc->sc_status = 0;
    187 	sc->sc_x = sc->sc_y = 0;
    188 
    189 	/* Enable interrupts. */
    190 	pms_dev_cmd(PMS_DEV_ENABLE);
    191 	pms_aux_cmd(PMS_AUX_ENABLE);
    192 	pms_dev_cmd(PMS_SET_RES);
    193 	pms_dev_cmd(3);		/* 8 counts/mm */
    194 	pms_dev_cmd(PMS_SET_SCALE21);
    195 #if 0
    196 	pms_dev_cmd(PMS_SET_SAMPLE);
    197 	pms_dev_cmd(100);	/* 100 samples/sec */
    198 	pms_dev_cmd(PMS_SET_STREAM);
    199 #endif
    200 	pms_pit_cmd(PMS_INT_ENABLE);
    201 
    202 	return 0;
    203 }
    204 
    205 int
    206 opmsclose(dev, flag, mode, p)
    207 	dev_t dev;
    208 	int flag;
    209 	int mode;
    210 	struct proc *p;
    211 {
    212 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    213 
    214 	/* Disable interrupts. */
    215 	pms_dev_cmd(PMS_DEV_DISABLE);
    216 	pms_pit_cmd(PMS_INT_DISABLE);
    217 	pms_aux_cmd(PMS_AUX_DISABLE);
    218 
    219 	sc->sc_state &= ~PMS_OPEN;
    220 
    221 	clfree(&sc->sc_q);
    222 
    223 	return 0;
    224 }
    225 
    226 int
    227 opmsread(dev, uio, flag)
    228 	dev_t dev;
    229 	struct uio *uio;
    230 	int flag;
    231 {
    232 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    233 	int s;
    234 	int error = 0;
    235 	size_t length;
    236 	u_char buffer[PMS_CHUNK];
    237 
    238 	/* Block until mouse activity occurred. */
    239 
    240 	s = spltty();
    241 	while (sc->sc_q.c_cc == 0) {
    242 		if (flag & IO_NDELAY) {
    243 			splx(s);
    244 			return EWOULDBLOCK;
    245 		}
    246 		sc->sc_state |= PMS_ASLP;
    247 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "pmsrea", 0);
    248 		if (error) {
    249 			sc->sc_state &= ~PMS_ASLP;
    250 			splx(s);
    251 			return error;
    252 		}
    253 	}
    254 	splx(s);
    255 
    256 	/* Transfer as many chunks as possible. */
    257 
    258 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    259 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    260 		if (length > sizeof(buffer))
    261 			length = sizeof(buffer);
    262 
    263 		/* Remove a small chunk from the input queue. */
    264 		(void) q_to_b(&sc->sc_q, buffer, length);
    265 
    266 		/* Copy the data to the user process. */
    267 		error = uiomove(buffer, length, uio);
    268 		if (error)
    269 			break;
    270 	}
    271 
    272 	return error;
    273 }
    274 
    275 int
    276 opmsioctl(dev, cmd, addr, flag, p)
    277 	dev_t dev;
    278 	u_long cmd;
    279 	caddr_t addr;
    280 	int flag;
    281 	struct proc *p;
    282 {
    283 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    284 	struct mouseinfo info;
    285 	int s;
    286 	int error;
    287 
    288 	switch (cmd) {
    289 	case MOUSEIOCREAD:
    290 		s = spltty();
    291 
    292 		info.status = sc->sc_status;
    293 		if (sc->sc_x || sc->sc_y)
    294 			info.status |= MOVEMENT;
    295 
    296 		if (sc->sc_x > 127)
    297 			info.xmotion = 127;
    298 		else if (sc->sc_x < -127)
    299 			/* Bounding at -127 avoids a bug in XFree86. */
    300 			info.xmotion = -127;
    301 		else
    302 			info.xmotion = sc->sc_x;
    303 
    304 		if (sc->sc_y > 127)
    305 			info.ymotion = 127;
    306 		else if (sc->sc_y < -127)
    307 			info.ymotion = -127;
    308 		else
    309 			info.ymotion = sc->sc_y;
    310 
    311 		/* Reset historical information. */
    312 		sc->sc_x = sc->sc_y = 0;
    313 		sc->sc_status &= ~BUTCHNGMASK;
    314 		ndflush(&sc->sc_q, sc->sc_q.c_cc);
    315 
    316 		splx(s);
    317 		error = copyout(&info, addr, sizeof(struct mouseinfo));
    318 		break;
    319 	default:
    320 		error = EINVAL;
    321 		break;
    322 	}
    323 
    324 	return error;
    325 }
    326 
    327 /* Masks for the first byte of a packet */
    328 #define PS2LBUTMASK 0x01
    329 #define PS2RBUTMASK 0x02
    330 #define PS2MBUTMASK 0x04
    331 
    332 int
    333 opmsintr(arg)
    334 	void *arg;
    335 {
    336 	struct opms_softc *sc = arg;
    337 	static int state = 0;
    338 	static u_char buttons;
    339 	u_char changed;
    340 	static char dx, dy;
    341 	u_char buffer[5];
    342 
    343 	if ((sc->sc_state & PMS_OPEN) == 0) {
    344 		/* Interrupts are not expected.  Discard the byte. */
    345 		kbd_flush_input();
    346 		return 0;
    347 	}
    348 
    349 	switch (state) {
    350 
    351 	case 0:
    352 		buttons = kbd_data_read_1();
    353 		if ((buttons & 0xc0) == 0)
    354 			++state;
    355 		break;
    356 
    357 	case 1:
    358 		dx = kbd_data_read_1();
    359 		/* Bounding at -127 avoids a bug in XFree86. */
    360 		dx = (dx == -128) ? -127 : dx;
    361 		++state;
    362 		break;
    363 
    364 	case 2:
    365 		dy = kbd_data_read_1();
    366 		dy = (dy == -128) ? -127 : dy;
    367 		state = 0;
    368 
    369 		buttons = ((buttons & PS2LBUTMASK) << 2) |
    370 			  ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
    371 		changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
    372 		sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
    373 
    374 		if (dx || dy || changed) {
    375 			/* Update accumulated movements. */
    376 			sc->sc_x += dx;
    377 			sc->sc_y += dy;
    378 
    379 			/* Add this event to the queue. */
    380 			buffer[0] = 0x80 | (buttons & BUTSTATMASK);
    381 			if(dx < 0)
    382 				buffer[0] |= 0x10;
    383 			buffer[1] = dx & 0x7f;
    384 			if(dy < 0)
    385 				buffer[0] |= 0x20;
    386 			buffer[2] = dy & 0x7f;
    387 			buffer[3] = buffer[4] = 0;
    388 			(void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
    389 
    390 			if (sc->sc_state & PMS_ASLP) {
    391 				sc->sc_state &= ~PMS_ASLP;
    392 				wakeup((caddr_t)sc);
    393 			}
    394 			selnotify(&sc->sc_rsel, 0);
    395 		}
    396 
    397 		break;
    398 	}
    399 	return -1;
    400 }
    401 
    402 int
    403 opmspoll(dev, events, p)
    404 	dev_t dev;
    405 	int events;
    406 	struct proc *p;
    407 {
    408 	struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
    409 	int revents = 0;
    410 	int s = spltty();
    411 
    412 	if (events & (POLLIN | POLLRDNORM)) {
    413 		if (sc->sc_q.c_cc > 0)
    414 			revents |= events & (POLLIN | POLLRDNORM);
    415 		else
    416 			selrecord(p, &sc->sc_rsel);
    417 	}
    418 
    419 	splx(s);
    420 	return (revents);
    421 }
    422 
    423 static void
    424 filt_opmsrdetach(struct knote *kn)
    425 {
    426 	struct opms_softc *sc = (void *) kn->kn_hook;
    427 	int s;
    428 
    429 	s = spltty();
    430 	SLIST_REMOVE(&sc->sc_rsel.si_klist, kn, knote, kn_selnext);
    431 	splx(s);
    432 }
    433 
    434 static int
    435 filt_opmsread(struct knote *kn, long hint)
    436 {
    437 	struct opms_softc *sc = (void *) kn->kn_hook;
    438 
    439 	kn->kn_data = sc->sc_q.c_cc;
    440 	return (kn->kn_data > 0);
    441 }
    442 
    443 static const struct filterops opmsread_filtops =
    444 	{ 1, NULL, filt_opmsrdetach, filt_opmsread };
    445 
    446 int
    447 opmskqfilter(dev_t dev, struct knote *kn)
    448 {
    449 	struct opms_softc *sc = opms_cd.cd_devs[LMSUNIT(dev)];
    450 	struct klist *klist;
    451 	int s;
    452 
    453 	switch (kn->kn_filter) {
    454 	case EVFILT_READ:
    455 		klist = &sc->sc_rsel.si_klist;
    456 		kn->kn_fop = &opmsread_filtops;
    457 		break;
    458 
    459 	default:
    460 		return (1);
    461 	}
    462 
    463 	kn->kn_hook = (void *) sc;
    464 
    465 	s = spltty();
    466 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    467 	splx(s);
    468 
    469 	return (0);
    470 }
    471