ms.c revision 1.9 1 /* $NetBSD: ms.c,v 1.9 2000/01/14 08:22:42 itohy 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 ms->ms_byteno = -1;
239 ms->ms_nodata = 0;
240
241 /* start sequencer */
242 ms_modem(ms);
243
244 return (0);
245 }
246
247 int
248 msclose(dev, flags, mode, p)
249 dev_t dev;
250 int flags, mode;
251 struct proc *p;
252 {
253 struct ms_softc *ms;
254
255 ms = ms_cd.cd_devs[minor(dev)];
256 ms->ms_ready = 0; /* stop accepting events */
257 untimeout(ms_modem, ms);
258 ev_fini(&ms->ms_events);
259
260 ms->ms_events.ev_io = NULL;
261 return (0);
262 }
263
264 int
265 msread(dev, uio, flags)
266 dev_t dev;
267 struct uio *uio;
268 int flags;
269 {
270 struct ms_softc *ms;
271
272 ms = ms_cd.cd_devs[minor(dev)];
273 return (ev_read(&ms->ms_events, uio, flags));
274 }
275
276 /* this routine should not exist, but is convenient to write here for now */
277 int
278 mswrite(dev, uio, flags)
279 dev_t dev;
280 struct uio *uio;
281 int flags;
282 {
283
284 return (EOPNOTSUPP);
285 }
286
287 int
288 msioctl(dev, cmd, data, flag, p)
289 dev_t dev;
290 u_long cmd;
291 register caddr_t data;
292 int flag;
293 struct proc *p;
294 {
295 struct ms_softc *ms;
296
297 ms = ms_cd.cd_devs[minor(dev)];
298
299 switch (cmd) {
300
301 case FIONBIO: /* we will remove this someday (soon???) */
302 return (0);
303
304 case FIOASYNC:
305 ms->ms_events.ev_async = *(int *)data != 0;
306 return (0);
307
308 case TIOCSPGRP:
309 if (*(int *)data != ms->ms_events.ev_io->p_pgid)
310 return (EPERM);
311 return (0);
312
313 case VUIDGFORMAT:
314 /* we only do firm_events */
315 *(int *)data = VUID_FIRM_EVENT;
316 return (0);
317
318 case VUIDSFORMAT:
319 if (*(int *)data != VUID_FIRM_EVENT)
320 return (EINVAL);
321 return (0);
322 }
323 return (ENOTTY);
324 }
325
326 int
327 mspoll(dev, events, p)
328 dev_t dev;
329 int events;
330 struct proc *p;
331 {
332 struct ms_softc *ms;
333
334 ms = ms_cd.cd_devs[minor(dev)];
335 return (ev_poll(&ms->ms_events, events, p));
336 }
337
338
339 /****************************************************************
340 * Middle layer (translator)
341 ****************************************************************/
342
343 static void ms_input __P((struct ms_softc *, int c));
344
345
346 /*
347 * Called by our ms_softint() routine on input.
348 */
349 static void
350 ms_input(ms, c)
351 register struct ms_softc *ms;
352 register int c;
353 {
354 register struct firm_event *fe;
355 register int mb, ub, d, get, put, any;
356 static const char to_one[] = { 1, 2, 3 };
357 static const int to_id[] = { MS_LEFT, MS_RIGHT, MS_MIDDLE };
358
359 /*
360 * Discard input if not ready. Drop sync on parity or framing
361 * error; gain sync on button byte.
362 */
363 if (ms->ms_ready == 0)
364 return;
365
366 ms->ms_nodata = 0;
367 /*
368 * Run the decode loop, adding to the current information.
369 * We add, rather than replace, deltas, so that if the event queue
370 * fills, we accumulate data for when it opens up again.
371 */
372 switch (ms->ms_byteno) {
373
374 case -1:
375 return;
376
377 case 0:
378 /* buttons */
379 ms->ms_byteno = 1;
380 ms->ms_mb = c & 0x3;
381 return;
382
383 case 1:
384 /* delta-x */
385 ms->ms_byteno = 2;
386 ms->ms_dx += (char)c;
387 return;
388
389 case 2:
390 /* delta-y */
391 ms->ms_byteno = -1;
392 ms->ms_dy += (char)c;
393 break;
394
395 default:
396 panic("ms_input");
397 /* NOTREACHED */
398 }
399
400 /*
401 * We have at least one event (mouse button, delta-X, or
402 * delta-Y; possibly all three, and possibly three separate
403 * button events). Deliver these events until we are out
404 * of changes or out of room. As events get delivered,
405 * mark them `unchanged'.
406 */
407 any = 0;
408 get = ms->ms_events.ev_get;
409 put = ms->ms_events.ev_put;
410 fe = &ms->ms_events.ev_q[put];
411
412 /* NEXT prepares to put the next event, backing off if necessary */
413 #define NEXT \
414 if ((++put) % EV_QSIZE == get) { \
415 put--; \
416 goto out; \
417 }
418 /* ADVANCE completes the `put' of the event */
419 #define ADVANCE \
420 fe++; \
421 if (put >= EV_QSIZE) { \
422 put = 0; \
423 fe = &ms->ms_events.ev_q[0]; \
424 } \
425
426 mb = ms->ms_mb;
427 ub = ms->ms_ub;
428 while ((d = mb ^ ub) != 0) {
429 /*
430 * Mouse button change. Convert up to three changes
431 * to the `first' change, and drop it into the event queue.
432 */
433 NEXT;
434 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
435 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
436 fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
437 fe->time = time;
438 ADVANCE;
439 ub ^= d;
440 any++;
441 }
442 if (ms->ms_dx) {
443 NEXT;
444 fe->id = LOC_X_DELTA;
445 fe->value = ms->ms_dx;
446 fe->time = time;
447 ADVANCE;
448 ms->ms_dx = 0;
449 any++;
450 }
451 if (ms->ms_dy) {
452 NEXT;
453 fe->id = LOC_Y_DELTA;
454 fe->value = -ms->ms_dy; /* XXX? */
455 fe->time = time;
456 ADVANCE;
457 ms->ms_dy = 0;
458 any++;
459 }
460 out:
461 if (any) {
462 ms->ms_ub = ub;
463 ms->ms_events.ev_put = put;
464 EV_WAKEUP(&ms->ms_events);
465 }
466 }
467
468 /****************************************************************
469 * Interface to the lower layer (zscc)
470 ****************************************************************/
471
472 static void ms_rxint __P((struct zs_chanstate *));
473 static void ms_stint __P((struct zs_chanstate *, int));
474 static void ms_txint __P((struct zs_chanstate *));
475 static void ms_softint __P((struct zs_chanstate *));
476
477 static void
478 ms_rxint(cs)
479 register struct zs_chanstate *cs;
480 {
481 register struct ms_softc *ms;
482 register int put, put_next;
483 register u_char c, rr1;
484
485 ms = cs->cs_private;
486 put = ms->ms_rbput;
487
488 /*
489 * First read the status, because reading the received char
490 * destroys the status of this char.
491 */
492 rr1 = zs_read_reg(cs, 1);
493 c = zs_read_data(cs);
494
495 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
496 /* Clear the receive error. */
497 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
498 }
499
500 ms->ms_rbuf[put] = (c << 8) | rr1;
501 put_next = (put + 1) & MS_RX_RING_MASK;
502
503 /* Would overrun if increment makes (put==get). */
504 if (put_next == ms->ms_rbget) {
505 ms->ms_intr_flags |= INTR_RX_OVERRUN;
506 } else {
507 /* OK, really increment. */
508 put = put_next;
509 }
510
511 /* Done reading. */
512 ms->ms_rbput = put;
513
514 /* Ask for softint() call. */
515 cs->cs_softreq = 1;
516 }
517
518
519 static void
520 ms_txint(cs)
521 register struct zs_chanstate *cs;
522 {
523 register struct ms_softc *ms;
524
525 ms = cs->cs_private;
526 zs_write_csr(cs, ZSWR0_RESET_TXINT);
527 ms->ms_intr_flags |= INTR_TX_EMPTY;
528 /* Ask for softint() call. */
529 cs->cs_softreq = 1;
530 }
531
532
533 static void
534 ms_stint(cs, force)
535 register struct zs_chanstate *cs;
536 int force;
537 {
538 register struct ms_softc *ms;
539 register int rr0;
540
541 ms = cs->cs_private;
542
543 rr0 = zs_read_csr(cs);
544 zs_write_csr(cs, ZSWR0_RESET_STATUS);
545
546 /*
547 * We have to accumulate status line changes here.
548 * Otherwise, if we get multiple status interrupts
549 * before the softint runs, we could fail to notice
550 * some status line changes in the softint routine.
551 * Fix from Bill Studenmund, October 1996.
552 */
553 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
554 cs->cs_rr0 = rr0;
555 ms->ms_intr_flags |= INTR_ST_CHECK;
556
557 /* Ask for softint() call. */
558 cs->cs_softreq = 1;
559 }
560
561
562 static void
563 ms_softint(cs)
564 struct zs_chanstate *cs;
565 {
566 register struct ms_softc *ms;
567 register int get, c, s;
568 int intr_flags;
569 register u_short ring_data;
570
571 ms = cs->cs_private;
572
573 /* Atomically get and clear flags. */
574 s = splzs();
575 intr_flags = ms->ms_intr_flags;
576 ms->ms_intr_flags = 0;
577
578 /* Now lower to spltty for the rest. */
579 (void) spltty();
580
581 /*
582 * Copy data from the receive ring to the event layer.
583 */
584 get = ms->ms_rbget;
585 while (get != ms->ms_rbput) {
586 ring_data = ms->ms_rbuf[get];
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 ms->ms_dev.dv_xname, ring_data);
597 c = -1; /* signal input error */
598 }
599
600 /* Pass this up to the "middle" layer. */
601 ms_input(ms, c);
602 }
603 if (intr_flags & INTR_RX_OVERRUN) {
604 log(LOG_ERR, "%s: input overrun\n",
605 ms->ms_dev.dv_xname);
606 }
607 ms->ms_rbget = get;
608
609 if (intr_flags & INTR_TX_EMPTY) {
610 /*
611 * Transmit done. (Not expected.)
612 */
613 log(LOG_ERR, "%s: transmit interrupt?\n",
614 ms->ms_dev.dv_xname);
615 }
616
617 if (intr_flags & INTR_ST_CHECK) {
618 /*
619 * Status line change. (Not expected.)
620 */
621 log(LOG_ERR, "%s: status interrupt?\n",
622 ms->ms_dev.dv_xname);
623 cs->cs_rr0_delta = 0;
624 }
625
626 splx(s);
627 }
628
629 struct zsops zsops_ms = {
630 ms_rxint, /* receive char available */
631 ms_stint, /* external/status */
632 ms_txint, /* xmit buffer empty */
633 ms_softint, /* process software interrupt */
634 };
635
636
637 static void
638 ms_trigger (cs, onoff)
639 struct zs_chanstate *cs;
640 int onoff;
641 {
642 /* for front connected one */
643 if (onoff)
644 cs->cs_preg[5] |= ZSWR5_RTS;
645 else
646 cs->cs_preg[5] &= ~ZSWR5_RTS;
647 cs->cs_creg[5] = cs->cs_preg[5];
648 zs_write_reg(cs, 5, cs->cs_preg[5]);
649
650 /* for keyborad connected one */
651 mfp_send_usart (onoff | 0x40);
652 }
653
654 /*
655 * mouse timer interrupt.
656 * called after system tick interrupt is done.
657 */
658 void
659 ms_modem(arg)
660 void *arg;
661 {
662 struct ms_softc *ms = arg;
663 int s;
664
665 if (!ms->ms_ready)
666 return;
667
668 s = splzs();
669
670 if (ms->ms_nodata++ > 250) { /* XXX */
671 log(LOG_ERR, "%s: no data for 5 secs. resetting.\n",
672 ms->ms_dev.dv_xname);
673 ms->ms_byteno = -1;
674 ms->ms_nodata = 0;
675 ms->ms_rts = 0;
676 }
677
678 if (ms->ms_rts) {
679 if (ms->ms_byteno == -1) {
680 /* start next sequence */
681 ms->ms_rts = 0;
682 ms_trigger(ms->ms_cs, ms->ms_rts);
683 ms->ms_byteno = 0;
684 }
685 } else {
686 ms->ms_rts = 1;
687 ms_trigger(ms->ms_cs, ms->ms_rts);
688 }
689
690 (void) splx(s);
691 timeout(ms_modem, ms, 2);
692 }
693