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