ms.c revision 1.14 1 /* $NetBSD: ms.c,v 1.14 2002/10/01 04:43:06 thorpej 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 CFATTACH_DECL(ms, sizeof(struct ms_softc),
143 ms_match, ms_attach, NULL, NULL)
144
145 extern struct zsops zsops_ms;
146 extern struct cfdriver ms_cd;
147
148 dev_type_open(msopen);
149 dev_type_close(msclose);
150 dev_type_read(msread);
151 dev_type_ioctl(msioctl);
152 dev_type_poll(mspoll);
153
154 const struct cdevsw ms_cdevsw ={
155 msopen, msclose, msread, nowrite, msioctl,
156 nostop, notty, mspoll, nommap,
157 };
158
159 /*
160 * ms_match: how is this zs channel configured?
161 */
162 int
163 ms_match(parent, cf, aux)
164 struct device *parent;
165 struct cfdata *cf;
166 void *aux;
167 {
168 struct zsc_attach_args *args = aux;
169 struct zsc_softc *zsc = (void*) parent;
170
171 /* Exact match required for the mouse. */
172 if (cf->cf_loc[ZSCCF_CHANNEL] != args->channel)
173 return 0;
174 if (args->channel != 1)
175 return 0;
176 if (&zsc->zsc_addr->zs_chan_b != (struct zschan *) ZSMS_PHYSADDR)
177 return 0;
178
179 return 2;
180 }
181
182 void
183 ms_attach(parent, self, aux)
184 struct device *parent, *self;
185 void *aux;
186
187 {
188 struct zsc_softc *zsc = (void *) parent;
189 struct ms_softc *ms = (void *) self;
190 struct zs_chanstate *cs;
191 struct cfdata *cf;
192 int reset, s;
193
194 callout_init(&ms->ms_modem_ch);
195
196 cf = ms->ms_dev.dv_cfdata;
197 cs = zsc->zsc_cs[1];
198 cs->cs_private = ms;
199 cs->cs_ops = &zsops_ms;
200 ms->ms_cs = cs;
201
202 /* Initialize the speed, etc. */
203 s = splzs();
204 /* May need reset... */
205 reset = ZSWR9_B_RESET;
206 zs_write_reg(cs, 9, reset);
207 /* We don't care about status or tx interrupts. */
208 cs->cs_preg[1] = ZSWR1_RIE;
209 cs->cs_preg[4] = ZSWR4_CLK_X16 | ZSWR4_TWOSB;
210 (void) zs_set_speed(cs, MS_BPS);
211 zs_loadchannelregs(cs);
212 splx(s);
213
214 /* Initialize translator. */
215 ms->ms_ready = 0;
216
217 printf ("\n");
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 ms->ms_rts = 1;
249 ms->ms_byteno = -1;
250 ms->ms_nodata = 0;
251
252 /* start sequencer */
253 ms_modem(ms);
254
255 return (0);
256 }
257
258 int
259 msclose(dev, flags, mode, p)
260 dev_t dev;
261 int flags, mode;
262 struct proc *p;
263 {
264 struct ms_softc *ms;
265
266 ms = ms_cd.cd_devs[minor(dev)];
267 ms->ms_ready = 0; /* stop accepting events */
268 callout_stop(&ms->ms_modem_ch);
269 ev_fini(&ms->ms_events);
270
271 ms->ms_events.ev_io = NULL;
272 return (0);
273 }
274
275 int
276 msread(dev, uio, flags)
277 dev_t dev;
278 struct uio *uio;
279 int flags;
280 {
281 struct ms_softc *ms;
282
283 ms = ms_cd.cd_devs[minor(dev)];
284 return (ev_read(&ms->ms_events, uio, flags));
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 callout_reset(&ms->ms_modem_ch, 2, ms_modem, ms);
692 }
693