xref: /netbsd-src/sys/dev/sun/ms.c (revision a5a68ff5f29de57339ca14f6c671c0a87714f1f8)
1 /*	$NetBSD: ms.c,v 1.12 1997/07/17 01:17:47 jtk Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratory.
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  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
45  */
46 
47 /*
48  * Mouse driver (/dev/mouse)
49  */
50 
51 /*
52  * Zilog Z8530 Dual UART driver (mouse interface)
53  *
54  * This is the "slave" driver that will be attached to
55  * the "zsc" driver for a Sun mouse.
56  */
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/conf.h>
61 #include <sys/device.h>
62 #include <sys/ioctl.h>
63 #include <sys/kernel.h>
64 #include <sys/proc.h>
65 #include <sys/signal.h>
66 #include <sys/signalvar.h>
67 #include <sys/time.h>
68 #include <sys/syslog.h>
69 #include <sys/select.h>
70 #include <sys/poll.h>
71 
72 #include <machine/vuid_event.h>
73 
74 #include <dev/ic/z8530reg.h>
75 #include <machine/z8530var.h>
76 
77 #include "event_var.h"
78 #include "locators.h"
79 
80 /*
81  * How many input characters we can buffer.
82  * The port-specific var.h may override this.
83  * Note: must be a power of two!
84  */
85 #define	MS_RX_RING_SIZE	256
86 #define MS_RX_RING_MASK (MS_RX_RING_SIZE-1)
87 /*
88  * Output buffer.  Only need a few chars.
89  */
90 #define	MS_TX_RING_SIZE	16
91 #define MS_TX_RING_MASK (MS_TX_RING_SIZE-1)
92 /*
93  * Keyboard serial line speed is fixed at 1200 bps.
94  */
95 #define MS_BPS 1200
96 
97 /*
98  * Mouse state.  A Mouse Systems mouse is a fairly simple device,
99  * producing five-byte blobs of the form:
100  *
101  *	b dx dy dx dy
102  *
103  * where b is the button state, encoded as 0x80|(~buttons)---there are
104  * three buttons (4=left, 2=middle, 1=right)---and dx,dy are X and Y
105  * delta values, none of which have are in [0x80..0x87].  (This lets
106  * us sync up with the mouse after an error.)
107  */
108 struct ms_softc {
109 	struct	device ms_dev;		/* required first: base device */
110 	struct	zs_chanstate *ms_cs;
111 
112 	/* Flags to communicate with ms_softintr() */
113 	volatile int ms_intr_flags;
114 #define	INTR_RX_OVERRUN 1
115 #define INTR_TX_EMPTY   2
116 #define INTR_ST_CHECK   4
117 
118 	/*
119 	 * The receive ring buffer.
120 	 */
121 	u_int	ms_rbget;	/* ring buffer `get' index */
122 	volatile u_int	ms_rbput;	/* ring buffer `put' index */
123 	u_short	ms_rbuf[MS_RX_RING_SIZE]; /* rr1, data pairs */
124 
125 	/*
126 	 * State of input translator
127 	 */
128 	short	ms_byteno;		/* input byte number, for decode */
129 	char	ms_mb;			/* mouse button state */
130 	char	ms_ub;			/* user button state */
131 	int	ms_dx;			/* delta-x */
132 	int	ms_dy;			/* delta-y */
133 
134 	/*
135 	 * State of upper interface.
136 	 */
137 	volatile int ms_ready;		/* event queue is ready */
138 	struct	evvar ms_events;	/* event queue state */
139 } ms_softc;
140 
141 cdev_decl(ms);	/* open, close, read, write, ioctl, stop, ... */
142 
143 struct zsops zsops_ms;
144 
145 /****************************************************************
146  * Definition of the driver for autoconfig.
147  ****************************************************************/
148 
149 static int	ms_match(struct device *, struct cfdata *, void *);
150 static void	ms_attach(struct device *, struct device *, void *);
151 
152 struct cfattach ms_ca = {
153 	sizeof(struct ms_softc), ms_match, ms_attach
154 };
155 
156 struct cfdriver ms_cd = {
157 	NULL, "ms", DV_DULL
158 };
159 
160 
161 /*
162  * ms_match: how is this zs channel configured?
163  */
164 int
165 ms_match(parent, cf, aux)
166 	struct device *parent;
167 	struct cfdata *cf;
168 	void   *aux;
169 {
170 	struct zsc_attach_args *args = aux;
171 
172 	/* Exact match required for keyboard. */
173 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
174 		return 2;
175 
176 	return 0;
177 }
178 
179 void
180 ms_attach(parent, self, aux)
181 	struct device *parent, *self;
182 	void   *aux;
183 
184 {
185 	struct zsc_softc *zsc = (void *) parent;
186 	struct ms_softc *ms = (void *) self;
187 	struct zsc_attach_args *args = aux;
188 	struct zs_chanstate *cs;
189 	struct cfdata *cf;
190 	int channel, ms_unit;
191 	int reset, s;
192 
193 	cf = ms->ms_dev.dv_cfdata;
194 	ms_unit = ms->ms_dev.dv_unit;
195 	channel = args->channel;
196 	cs = zsc->zsc_cs[channel];
197 	cs->cs_private = ms;
198 	cs->cs_ops = &zsops_ms;
199 	ms->ms_cs = cs;
200 
201 	printf("\n");
202 
203 	/* Initialize the speed, etc. */
204 	s = splzs();
205 	/* May need reset... */
206 	reset = (channel == 0) ?
207 		ZSWR9_A_RESET : ZSWR9_B_RESET;
208 	zs_write_reg(cs, 9, reset);
209 	/* These are OK as set by zscc: WR3, WR4, WR5 */
210 	/* We don't care about status or tx interrupts. */
211 	cs->cs_preg[1] = ZSWR1_RIE;
212 	(void) zs_set_speed(cs, MS_BPS);
213 	zs_loadchannelregs(cs);
214 	splx(s);
215 
216 	/* Initialize translator. */
217 	ms->ms_byteno = -1;
218 }
219 
220 /****************************************************************
221  *  Entry points for /dev/mouse
222  *  (open,close,read,write,...)
223  ****************************************************************/
224 
225 int
226 msopen(dev, flags, mode, p)
227 	dev_t dev;
228 	int flags, mode;
229 	struct proc *p;
230 {
231 	struct ms_softc *ms;
232 	int unit;
233 
234 	unit = minor(dev);
235 	if (unit >= ms_cd.cd_ndevs)
236 		return (ENXIO);
237 	ms = ms_cd.cd_devs[unit];
238 	if (ms == NULL)
239 		return (ENXIO);
240 
241 	/* This is an exclusive open device. */
242 	if (ms->ms_events.ev_io)
243 		return (EBUSY);
244 	ms->ms_events.ev_io = p;
245 	ev_init(&ms->ms_events);	/* may cause sleep */
246 
247 	ms->ms_ready = 1;		/* start accepting events */
248 	return (0);
249 }
250 
251 int
252 msclose(dev, flags, mode, p)
253 	dev_t dev;
254 	int flags, mode;
255 	struct proc *p;
256 {
257 	struct ms_softc *ms;
258 
259 	ms = ms_cd.cd_devs[minor(dev)];
260 	ms->ms_ready = 0;		/* stop accepting events */
261 	ev_fini(&ms->ms_events);
262 
263 	ms->ms_events.ev_io = NULL;
264 	return (0);
265 }
266 
267 int
268 msread(dev, uio, flags)
269 	dev_t dev;
270 	struct uio *uio;
271 	int flags;
272 {
273 	struct ms_softc *ms;
274 
275 	ms = ms_cd.cd_devs[minor(dev)];
276 	return (ev_read(&ms->ms_events, uio, flags));
277 }
278 
279 /* this routine should not exist, but is convenient to write here for now */
280 int
281 mswrite(dev, uio, flags)
282 	dev_t dev;
283 	struct uio *uio;
284 	int flags;
285 {
286 
287 	return (EOPNOTSUPP);
288 }
289 
290 int
291 msioctl(dev, cmd, data, flag, p)
292 	dev_t dev;
293 	u_long cmd;
294 	register caddr_t data;
295 	int flag;
296 	struct proc *p;
297 {
298 	struct ms_softc *ms;
299 
300 	ms = ms_cd.cd_devs[minor(dev)];
301 
302 	switch (cmd) {
303 
304 	case FIONBIO:		/* we will remove this someday (soon???) */
305 		return (0);
306 
307 	case FIOASYNC:
308 		ms->ms_events.ev_async = *(int *)data != 0;
309 		return (0);
310 
311 	case TIOCSPGRP:
312 		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
313 			return (EPERM);
314 		return (0);
315 
316 	case VUIDGFORMAT:
317 		/* we only do firm_events */
318 		*(int *)data = VUID_FIRM_EVENT;
319 		return (0);
320 
321 	case VUIDSFORMAT:
322 		if (*(int *)data != VUID_FIRM_EVENT)
323 			return (EINVAL);
324 		return (0);
325 	}
326 	return (ENOTTY);
327 }
328 
329 int
330 mspoll(dev, events, p)
331 	dev_t dev;
332 	int events;
333 	struct proc *p;
334 {
335 	struct ms_softc *ms;
336 
337 	ms = ms_cd.cd_devs[minor(dev)];
338 	return (ev_poll(&ms->ms_events, events, p));
339 }
340 
341 
342 /****************************************************************
343  * Middle layer (translator)
344  ****************************************************************/
345 
346 static void ms_input __P((struct ms_softc *, int c));
347 
348 
349 /*
350  * Called by our ms_softint() routine on input.
351  */
352 void
353 ms_input(ms, c)
354 	register struct ms_softc *ms;
355 	register int c;
356 {
357 	register struct firm_event *fe;
358 	register int mb, ub, d, get, put, any;
359 	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
360 	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
361 
362 	/*
363 	 * Discard input if not ready.  Drop sync on parity or framing
364 	 * error; gain sync on button byte.
365 	 */
366 	if (ms->ms_ready == 0)
367 		return;
368 	if (c == -1) {
369 		ms->ms_byteno = -1;
370 		return;
371 	}
372 	if ((c & ~7) == 0x80)	/* if in 0x80..0x87 */
373 		ms->ms_byteno = 0;
374 
375 	/*
376 	 * Run the decode loop, adding to the current information.
377 	 * We add, rather than replace, deltas, so that if the event queue
378 	 * fills, we accumulate data for when it opens up again.
379 	 */
380 	switch (ms->ms_byteno) {
381 
382 	case -1:
383 		return;
384 
385 	case 0:
386 		/* buttons */
387 		ms->ms_byteno = 1;
388 		ms->ms_mb = (~c) & 0x7;
389 		return;
390 
391 	case 1:
392 		/* first delta-x */
393 		ms->ms_byteno = 2;
394 		ms->ms_dx += (char)c;
395 		return;
396 
397 	case 2:
398 		/* first delta-y */
399 		ms->ms_byteno = 3;
400 		ms->ms_dy += (char)c;
401 		return;
402 
403 	case 3:
404 		/* second delta-x */
405 		ms->ms_byteno = 4;
406 		ms->ms_dx += (char)c;
407 		return;
408 
409 	case 4:
410 		/* second delta-x */
411 		ms->ms_byteno = -1;	/* wait for button-byte again */
412 		ms->ms_dy += (char)c;
413 		break;
414 
415 	default:
416 		panic("ms_rint");
417 		/* NOTREACHED */
418 	}
419 
420 	/*
421 	 * We have at least one event (mouse button, delta-X, or
422 	 * delta-Y; possibly all three, and possibly three separate
423 	 * button events).  Deliver these events until we are out
424 	 * of changes or out of room.  As events get delivered,
425 	 * mark them `unchanged'.
426 	 */
427 	any = 0;
428 	get = ms->ms_events.ev_get;
429 	put = ms->ms_events.ev_put;
430 	fe = &ms->ms_events.ev_q[put];
431 
432 	/* NEXT prepares to put the next event, backing off if necessary */
433 #define	NEXT \
434 	if ((++put) % EV_QSIZE == get) { \
435 		put--; \
436 		goto out; \
437 	}
438 	/* ADVANCE completes the `put' of the event */
439 #define	ADVANCE \
440 	fe++; \
441 	if (put >= EV_QSIZE) { \
442 		put = 0; \
443 		fe = &ms->ms_events.ev_q[0]; \
444 	} \
445 	any = 1
446 
447 	mb = ms->ms_mb;
448 	ub = ms->ms_ub;
449 	while ((d = mb ^ ub) != 0) {
450 		/*
451 		 * Mouse button change.  Convert up to three changes
452 		 * to the `first' change, and drop it into the event queue.
453 		 */
454 		NEXT;
455 		d = to_one[d - 1];		/* from 1..7 to {1,2,4} */
456 		fe->id = to_id[d - 1];		/* from {1,2,4} to ID */
457 		fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
458 		fe->time = time;
459 		ADVANCE;
460 		ub ^= d;
461 	}
462 	if (ms->ms_dx) {
463 		NEXT;
464 		fe->id = LOC_X_DELTA;
465 		fe->value = ms->ms_dx;
466 		fe->time = time;
467 		ADVANCE;
468 		ms->ms_dx = 0;
469 	}
470 	if (ms->ms_dy) {
471 		NEXT;
472 		fe->id = LOC_Y_DELTA;
473 		fe->value = ms->ms_dy;
474 		fe->time = time;
475 		ADVANCE;
476 		ms->ms_dy = 0;
477 	}
478 out:
479 	if (any) {
480 		ms->ms_ub = ub;
481 		ms->ms_events.ev_put = put;
482 		EV_WAKEUP(&ms->ms_events);
483 	}
484 }
485 
486 /****************************************************************
487  * Interface to the lower layer (zscc)
488  ****************************************************************/
489 
490 static void ms_rxint __P((struct zs_chanstate *));
491 static void ms_txint __P((struct zs_chanstate *));
492 static void ms_stint __P((struct zs_chanstate *));
493 static void ms_softint __P((struct zs_chanstate *));
494 
495 static void
496 ms_rxint(cs)
497 	register struct zs_chanstate *cs;
498 {
499 	register struct ms_softc *ms;
500 	register int put, put_next;
501 	register u_char c, rr1;
502 
503 	ms = cs->cs_private;
504 	put = ms->ms_rbput;
505 
506 	/*
507 	 * First read the status, because reading the received char
508 	 * destroys the status of this char.
509 	 */
510 	rr1 = zs_read_reg(cs, 1);
511 	c = zs_read_data(cs);
512 
513 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
514 		/* Clear the receive error. */
515 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
516 	}
517 
518 	ms->ms_rbuf[put] = (c << 8) | rr1;
519 	put_next = (put + 1) & MS_RX_RING_MASK;
520 
521 	/* Would overrun if increment makes (put==get). */
522 	if (put_next == ms->ms_rbget) {
523 		ms->ms_intr_flags |= INTR_RX_OVERRUN;
524 	} else {
525 		/* OK, really increment. */
526 		put = put_next;
527 	}
528 
529 	/* Done reading. */
530 	ms->ms_rbput = put;
531 
532 	/* Ask for softint() call. */
533 	cs->cs_softreq = 1;
534 }
535 
536 
537 static void
538 ms_txint(cs)
539 	register struct zs_chanstate *cs;
540 {
541 	register struct ms_softc *ms;
542 
543 	ms = cs->cs_private;
544 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
545 	ms->ms_intr_flags |= INTR_TX_EMPTY;
546 	/* Ask for softint() call. */
547 	cs->cs_softreq = 1;
548 }
549 
550 
551 static void
552 ms_stint(cs)
553 	register struct zs_chanstate *cs;
554 {
555 	register struct ms_softc *ms;
556 	register int rr0;
557 
558 	ms = cs->cs_private;
559 
560 	rr0 = zs_read_csr(cs);
561 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
562 
563 	/*
564 	 * We have to accumulate status line changes here.
565 	 * Otherwise, if we get multiple status interrupts
566 	 * before the softint runs, we could fail to notice
567 	 * some status line changes in the softint routine.
568 	 * Fix from Bill Studenmund, October 1996.
569 	 */
570 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
571 	cs->cs_rr0 = rr0;
572 	ms->ms_intr_flags |= INTR_ST_CHECK;
573 
574 	/* Ask for softint() call. */
575 	cs->cs_softreq = 1;
576 }
577 
578 
579 static void
580 ms_softint(cs)
581 	struct zs_chanstate *cs;
582 {
583 	register struct ms_softc *ms;
584 	register int get, c, s;
585 	int intr_flags;
586 	register u_short ring_data;
587 
588 	ms = cs->cs_private;
589 
590 	/* Atomically get and clear flags. */
591 	s = splzs();
592 	intr_flags = ms->ms_intr_flags;
593 	ms->ms_intr_flags = 0;
594 
595 	/* Now lower to spltty for the rest. */
596 	(void) spltty();
597 
598 	/*
599 	 * Copy data from the receive ring to the event layer.
600 	 */
601 	get = ms->ms_rbget;
602 	while (get != ms->ms_rbput) {
603 		ring_data = ms->ms_rbuf[get];
604 		get = (get + 1) & MS_RX_RING_MASK;
605 
606 		/* low byte of ring_data is rr1 */
607 		c = (ring_data >> 8) & 0xff;
608 
609 		if (ring_data & ZSRR1_DO)
610 			intr_flags |= INTR_RX_OVERRUN;
611 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
612 			log(LOG_ERR, "%s: input error (0x%x)\n",
613 				ms->ms_dev.dv_xname, ring_data);
614 			c = -1;	/* signal input error */
615 		}
616 
617 		/* Pass this up to the "middle" layer. */
618 		ms_input(ms, c);
619 	}
620 	if (intr_flags & INTR_RX_OVERRUN) {
621 		log(LOG_ERR, "%s: input overrun\n",
622 		    ms->ms_dev.dv_xname);
623 	}
624 	ms->ms_rbget = get;
625 
626 	if (intr_flags & INTR_TX_EMPTY) {
627 		/*
628 		 * Transmit done.  (Not expected.)
629 		 */
630 		log(LOG_ERR, "%s: transmit interrupt?\n",
631 		    ms->ms_dev.dv_xname);
632 	}
633 
634 	if (intr_flags & INTR_ST_CHECK) {
635 		/*
636 		 * Status line change.  (Not expected.)
637 		 */
638 		log(LOG_ERR, "%s: status interrupt?\n",
639 		    ms->ms_dev.dv_xname);
640 		cs->cs_rr0_delta = 0;
641 	}
642 
643 	splx(s);
644 }
645 
646 struct zsops zsops_ms = {
647 	ms_rxint,	/* receive char available */
648 	ms_stint,	/* external/status */
649 	ms_txint,	/* xmit buffer empty */
650 	ms_softint,	/* process software interrupt */
651 };
652