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