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