ms.c revision 1.16 1 /* $NetBSD: ms.c,v 1.16 2002/10/23 09:12:46 jdolecek 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 dev_type_kqfilter(mskqfilter);
154
155 const struct cdevsw ms_cdevsw ={
156 msopen, msclose, msread, nowrite, msioctl,
157 nostop, notty, mspoll, nommap, mskqfilter,
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 int
340 mskqfilter(dev_t dev, struct knote *kn)
341 {
342 struct ms_softc *ms;
343
344 ms = ms_cd.cd_devs[minor(dev)];
345 return (ev_kqfilter(&ms->ms_events, kn));
346 }
347
348 /****************************************************************
349 * Middle layer (translator)
350 ****************************************************************/
351
352 static void ms_input __P((struct ms_softc *, int c));
353
354
355 /*
356 * Called by our ms_softint() routine on input.
357 */
358 static void
359 ms_input(ms, c)
360 register struct ms_softc *ms;
361 register int c;
362 {
363 register struct firm_event *fe;
364 register int mb, ub, d, get, put, any;
365 static const char to_one[] = { 1, 2, 3 };
366 static const int to_id[] = { MS_LEFT, MS_RIGHT, MS_MIDDLE };
367
368 /*
369 * Discard input if not ready. Drop sync on parity or framing
370 * error; gain sync on button byte.
371 */
372 if (ms->ms_ready == 0)
373 return;
374
375 ms->ms_nodata = 0;
376 /*
377 * Run the decode loop, adding to the current information.
378 * We add, rather than replace, deltas, so that if the event queue
379 * fills, we accumulate data for when it opens up again.
380 */
381 switch (ms->ms_byteno) {
382
383 case -1:
384 return;
385
386 case 0:
387 /* buttons */
388 ms->ms_byteno = 1;
389 ms->ms_mb = c & 0x3;
390 return;
391
392 case 1:
393 /* delta-x */
394 ms->ms_byteno = 2;
395 ms->ms_dx += (char)c;
396 return;
397
398 case 2:
399 /* delta-y */
400 ms->ms_byteno = -1;
401 ms->ms_dy += (char)c;
402 break;
403
404 default:
405 panic("ms_input");
406 /* NOTREACHED */
407 }
408
409 /*
410 * We have at least one event (mouse button, delta-X, or
411 * delta-Y; possibly all three, and possibly three separate
412 * button events). Deliver these events until we are out
413 * of changes or out of room. As events get delivered,
414 * mark them `unchanged'.
415 */
416 any = 0;
417 get = ms->ms_events.ev_get;
418 put = ms->ms_events.ev_put;
419 fe = &ms->ms_events.ev_q[put];
420
421 /* NEXT prepares to put the next event, backing off if necessary */
422 #define NEXT \
423 if ((++put) % EV_QSIZE == get) { \
424 put--; \
425 goto out; \
426 }
427 /* ADVANCE completes the `put' of the event */
428 #define ADVANCE \
429 fe++; \
430 if (put >= EV_QSIZE) { \
431 put = 0; \
432 fe = &ms->ms_events.ev_q[0]; \
433 } \
434
435 mb = ms->ms_mb;
436 ub = ms->ms_ub;
437 while ((d = mb ^ ub) != 0) {
438 /*
439 * Mouse button change. Convert up to three changes
440 * to the `first' change, and drop it into the event queue.
441 */
442 NEXT;
443 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
444 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
445 fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
446 fe->time = time;
447 ADVANCE;
448 ub ^= d;
449 any++;
450 }
451 if (ms->ms_dx) {
452 NEXT;
453 fe->id = LOC_X_DELTA;
454 fe->value = ms->ms_dx;
455 fe->time = time;
456 ADVANCE;
457 ms->ms_dx = 0;
458 any++;
459 }
460 if (ms->ms_dy) {
461 NEXT;
462 fe->id = LOC_Y_DELTA;
463 fe->value = -ms->ms_dy; /* XXX? */
464 fe->time = time;
465 ADVANCE;
466 ms->ms_dy = 0;
467 any++;
468 }
469 out:
470 if (any) {
471 ms->ms_ub = ub;
472 ms->ms_events.ev_put = put;
473 EV_WAKEUP(&ms->ms_events);
474 }
475 }
476
477 /****************************************************************
478 * Interface to the lower layer (zscc)
479 ****************************************************************/
480
481 static void ms_rxint __P((struct zs_chanstate *));
482 static void ms_stint __P((struct zs_chanstate *, int));
483 static void ms_txint __P((struct zs_chanstate *));
484 static void ms_softint __P((struct zs_chanstate *));
485
486 static void
487 ms_rxint(cs)
488 register struct zs_chanstate *cs;
489 {
490 register struct ms_softc *ms;
491 register int put, put_next;
492 register u_char c, rr1;
493
494 ms = cs->cs_private;
495 put = ms->ms_rbput;
496
497 /*
498 * First read the status, because reading the received char
499 * destroys the status of this char.
500 */
501 rr1 = zs_read_reg(cs, 1);
502 c = zs_read_data(cs);
503
504 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
505 /* Clear the receive error. */
506 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
507 }
508
509 ms->ms_rbuf[put] = (c << 8) | rr1;
510 put_next = (put + 1) & MS_RX_RING_MASK;
511
512 /* Would overrun if increment makes (put==get). */
513 if (put_next == ms->ms_rbget) {
514 ms->ms_intr_flags |= INTR_RX_OVERRUN;
515 } else {
516 /* OK, really increment. */
517 put = put_next;
518 }
519
520 /* Done reading. */
521 ms->ms_rbput = put;
522
523 /* Ask for softint() call. */
524 cs->cs_softreq = 1;
525 }
526
527
528 static void
529 ms_txint(cs)
530 register struct zs_chanstate *cs;
531 {
532 register struct ms_softc *ms;
533
534 ms = cs->cs_private;
535 zs_write_csr(cs, ZSWR0_RESET_TXINT);
536 ms->ms_intr_flags |= INTR_TX_EMPTY;
537 /* Ask for softint() call. */
538 cs->cs_softreq = 1;
539 }
540
541
542 static void
543 ms_stint(cs, force)
544 register struct zs_chanstate *cs;
545 int force;
546 {
547 register struct ms_softc *ms;
548 register int rr0;
549
550 ms = cs->cs_private;
551
552 rr0 = zs_read_csr(cs);
553 zs_write_csr(cs, ZSWR0_RESET_STATUS);
554
555 /*
556 * We have to accumulate status line changes here.
557 * Otherwise, if we get multiple status interrupts
558 * before the softint runs, we could fail to notice
559 * some status line changes in the softint routine.
560 * Fix from Bill Studenmund, October 1996.
561 */
562 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
563 cs->cs_rr0 = rr0;
564 ms->ms_intr_flags |= INTR_ST_CHECK;
565
566 /* Ask for softint() call. */
567 cs->cs_softreq = 1;
568 }
569
570
571 static void
572 ms_softint(cs)
573 struct zs_chanstate *cs;
574 {
575 register struct ms_softc *ms;
576 register int get, c, s;
577 int intr_flags;
578 register u_short ring_data;
579
580 ms = cs->cs_private;
581
582 /* Atomically get and clear flags. */
583 s = splzs();
584 intr_flags = ms->ms_intr_flags;
585 ms->ms_intr_flags = 0;
586
587 /* Now lower to spltty for the rest. */
588 (void) spltty();
589
590 /*
591 * Copy data from the receive ring to the event layer.
592 */
593 get = ms->ms_rbget;
594 while (get != ms->ms_rbput) {
595 ring_data = ms->ms_rbuf[get];
596 get = (get + 1) & MS_RX_RING_MASK;
597
598 /* low byte of ring_data is rr1 */
599 c = (ring_data >> 8) & 0xff;
600
601 if (ring_data & ZSRR1_DO)
602 intr_flags |= INTR_RX_OVERRUN;
603 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
604 log(LOG_ERR, "%s: input error (0x%x)\n",
605 ms->ms_dev.dv_xname, ring_data);
606 c = -1; /* signal input error */
607 }
608
609 /* Pass this up to the "middle" layer. */
610 ms_input(ms, c);
611 }
612 if (intr_flags & INTR_RX_OVERRUN) {
613 log(LOG_ERR, "%s: input overrun\n",
614 ms->ms_dev.dv_xname);
615 }
616 ms->ms_rbget = get;
617
618 if (intr_flags & INTR_TX_EMPTY) {
619 /*
620 * Transmit done. (Not expected.)
621 */
622 log(LOG_ERR, "%s: transmit interrupt?\n",
623 ms->ms_dev.dv_xname);
624 }
625
626 if (intr_flags & INTR_ST_CHECK) {
627 /*
628 * Status line change. (Not expected.)
629 */
630 log(LOG_ERR, "%s: status interrupt?\n",
631 ms->ms_dev.dv_xname);
632 cs->cs_rr0_delta = 0;
633 }
634
635 splx(s);
636 }
637
638 struct zsops zsops_ms = {
639 ms_rxint, /* receive char available */
640 ms_stint, /* external/status */
641 ms_txint, /* xmit buffer empty */
642 ms_softint, /* process software interrupt */
643 };
644
645
646 static void
647 ms_trigger (cs, onoff)
648 struct zs_chanstate *cs;
649 int onoff;
650 {
651 /* for front connected one */
652 if (onoff)
653 cs->cs_preg[5] |= ZSWR5_RTS;
654 else
655 cs->cs_preg[5] &= ~ZSWR5_RTS;
656 cs->cs_creg[5] = cs->cs_preg[5];
657 zs_write_reg(cs, 5, cs->cs_preg[5]);
658
659 /* for keyborad connected one */
660 mfp_send_usart (onoff | 0x40);
661 }
662
663 /*
664 * mouse timer interrupt.
665 * called after system tick interrupt is done.
666 */
667 void
668 ms_modem(arg)
669 void *arg;
670 {
671 struct ms_softc *ms = arg;
672 int s;
673
674 if (!ms->ms_ready)
675 return;
676
677 s = splzs();
678
679 if (ms->ms_nodata++ > 250) { /* XXX */
680 log(LOG_ERR, "%s: no data for 5 secs. resetting.\n",
681 ms->ms_dev.dv_xname);
682 ms->ms_byteno = -1;
683 ms->ms_nodata = 0;
684 ms->ms_rts = 0;
685 }
686
687 if (ms->ms_rts) {
688 if (ms->ms_byteno == -1) {
689 /* start next sequence */
690 ms->ms_rts = 0;
691 ms_trigger(ms->ms_cs, ms->ms_rts);
692 ms->ms_byteno = 0;
693 }
694 } else {
695 ms->ms_rts = 1;
696 ms_trigger(ms->ms_cs, ms->ms_rts);
697 }
698
699 (void) splx(s);
700 callout_reset(&ms->ms_modem_ch, 2, ms_modem, ms);
701 }
702