ms.c revision 1.18 1 /* $NetBSD: ms.c,v 1.18 2003/08/07 16:30:25 agc 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.18 2003/08/07 16:30:25 agc 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
61 #include <dev/ic/z8530reg.h>
62 #include <machine/z8530var.h>
63
64 #include <arch/x68k/dev/event_var.h>
65 #include <machine/vuid_event.h>
66 #include <arch/x68k/dev/mfp.h>
67
68 #include "locators.h"
69
70 /*
71 * How many input characters we can buffer.
72 * The port-specific var.h may override this.
73 * Note: must be a power of two!
74 */
75 #define MS_RX_RING_SIZE 256
76 #define MS_RX_RING_MASK (MS_RX_RING_SIZE-1)
77 /*
78 * Output buffer. Only need a few chars.
79 */
80 #define MS_TX_RING_SIZE 16
81 #define MS_TX_RING_MASK (MS_TX_RING_SIZE-1)
82 /*
83 * Mouse serial line is fixed at 4800 bps.
84 */
85 #define MS_BPS 4800
86
87 /*
88 * Mouse state. A SHARP X1/X680x0 mouse is a fairly simple device,
89 * producing three-byte blobs of the form:
90 *
91 * b dx dy
92 *
93 * where b is the button state, encoded as 0x80|(buttons)---there are
94 * two buttons (2=left, 1=right)---and dx,dy are X and Y delta values.
95 *
96 * It needs a trigger for the transmission. When zs RTS negated, the
97 * mouse begins the sequence. RTS assertion has no effect.
98 */
99 struct ms_softc {
100 struct device ms_dev; /* required first: base device */
101 struct zs_chanstate *ms_cs;
102
103 struct callout ms_modem_ch;
104
105 /* Flags to communicate with ms_softintr() */
106 volatile int ms_intr_flags;
107 #define INTR_RX_OVERRUN 1
108 #define INTR_TX_EMPTY 2
109 #define INTR_ST_CHECK 4
110
111 /*
112 * The receive ring buffer.
113 */
114 u_int ms_rbget; /* ring buffer `get' index */
115 volatile u_int ms_rbput; /* ring buffer `put' index */
116 u_short ms_rbuf[MS_RX_RING_SIZE]; /* rr1, data pairs */
117
118 /*
119 * State of input translator
120 */
121 short ms_byteno; /* input byte number, for decode */
122 char ms_mb; /* mouse button state */
123 char ms_ub; /* user button state */
124 int ms_dx; /* delta-x */
125 int ms_dy; /* delta-y */
126 int ms_rts; /* MSCTRL */
127 int ms_nodata;
128
129 /*
130 * State of upper interface.
131 */
132 volatile int ms_ready; /* event queue is ready */
133 struct evvar ms_events; /* event queue state */
134 } ms_softc;
135
136 static int ms_match __P((struct device*, struct cfdata*, void*));
137 static void ms_attach __P((struct device*, struct device*, void*));
138 static void ms_trigger __P((struct zs_chanstate*, int));
139 void ms_modem __P((void *));
140
141 CFATTACH_DECL(ms, sizeof(struct ms_softc),
142 ms_match, ms_attach, NULL, NULL);
143
144 extern struct zsops zsops_ms;
145 extern struct cfdriver ms_cd;
146
147 dev_type_open(msopen);
148 dev_type_close(msclose);
149 dev_type_read(msread);
150 dev_type_ioctl(msioctl);
151 dev_type_poll(mspoll);
152 dev_type_kqfilter(mskqfilter);
153
154 const struct cdevsw ms_cdevsw ={
155 msopen, msclose, msread, nowrite, msioctl,
156 nostop, notty, mspoll, nommap, mskqfilter,
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 int
339 mskqfilter(dev_t dev, struct knote *kn)
340 {
341 struct ms_softc *ms;
342
343 ms = ms_cd.cd_devs[minor(dev)];
344 return (ev_kqfilter(&ms->ms_events, kn));
345 }
346
347 /****************************************************************
348 * Middle layer (translator)
349 ****************************************************************/
350
351 static void ms_input __P((struct ms_softc *, int c));
352
353
354 /*
355 * Called by our ms_softint() routine on input.
356 */
357 static void
358 ms_input(ms, c)
359 register struct ms_softc *ms;
360 register int c;
361 {
362 register struct firm_event *fe;
363 register int mb, ub, d, get, put, any;
364 static const char to_one[] = { 1, 2, 3 };
365 static const int to_id[] = { MS_LEFT, MS_RIGHT, MS_MIDDLE };
366
367 /*
368 * Discard input if not ready. Drop sync on parity or framing
369 * error; gain sync on button byte.
370 */
371 if (ms->ms_ready == 0)
372 return;
373
374 ms->ms_nodata = 0;
375 /*
376 * Run the decode loop, adding to the current information.
377 * We add, rather than replace, deltas, so that if the event queue
378 * fills, we accumulate data for when it opens up again.
379 */
380 switch (ms->ms_byteno) {
381
382 case -1:
383 return;
384
385 case 0:
386 /* buttons */
387 ms->ms_byteno = 1;
388 ms->ms_mb = c & 0x3;
389 return;
390
391 case 1:
392 /* delta-x */
393 ms->ms_byteno = 2;
394 ms->ms_dx += (char)c;
395 return;
396
397 case 2:
398 /* delta-y */
399 ms->ms_byteno = -1;
400 ms->ms_dy += (char)c;
401 break;
402
403 default:
404 panic("ms_input");
405 /* NOTREACHED */
406 }
407
408 /*
409 * We have at least one event (mouse button, delta-X, or
410 * delta-Y; possibly all three, and possibly three separate
411 * button events). Deliver these events until we are out
412 * of changes or out of room. As events get delivered,
413 * mark them `unchanged'.
414 */
415 any = 0;
416 get = ms->ms_events.ev_get;
417 put = ms->ms_events.ev_put;
418 fe = &ms->ms_events.ev_q[put];
419
420 /* NEXT prepares to put the next event, backing off if necessary */
421 #define NEXT \
422 if ((++put) % EV_QSIZE == get) { \
423 put--; \
424 goto out; \
425 }
426 /* ADVANCE completes the `put' of the event */
427 #define ADVANCE \
428 fe++; \
429 if (put >= EV_QSIZE) { \
430 put = 0; \
431 fe = &ms->ms_events.ev_q[0]; \
432 } \
433
434 mb = ms->ms_mb;
435 ub = ms->ms_ub;
436 while ((d = mb ^ ub) != 0) {
437 /*
438 * Mouse button change. Convert up to three changes
439 * to the `first' change, and drop it into the event queue.
440 */
441 NEXT;
442 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
443 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
444 fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
445 fe->time = time;
446 ADVANCE;
447 ub ^= d;
448 any++;
449 }
450 if (ms->ms_dx) {
451 NEXT;
452 fe->id = LOC_X_DELTA;
453 fe->value = ms->ms_dx;
454 fe->time = time;
455 ADVANCE;
456 ms->ms_dx = 0;
457 any++;
458 }
459 if (ms->ms_dy) {
460 NEXT;
461 fe->id = LOC_Y_DELTA;
462 fe->value = -ms->ms_dy; /* XXX? */
463 fe->time = time;
464 ADVANCE;
465 ms->ms_dy = 0;
466 any++;
467 }
468 out:
469 if (any) {
470 ms->ms_ub = ub;
471 ms->ms_events.ev_put = put;
472 EV_WAKEUP(&ms->ms_events);
473 }
474 }
475
476 /****************************************************************
477 * Interface to the lower layer (zscc)
478 ****************************************************************/
479
480 static void ms_rxint __P((struct zs_chanstate *));
481 static void ms_stint __P((struct zs_chanstate *, int));
482 static void ms_txint __P((struct zs_chanstate *));
483 static void ms_softint __P((struct zs_chanstate *));
484
485 static void
486 ms_rxint(cs)
487 register struct zs_chanstate *cs;
488 {
489 register struct ms_softc *ms;
490 register int put, put_next;
491 register u_char c, rr1;
492
493 ms = cs->cs_private;
494 put = ms->ms_rbput;
495
496 /*
497 * First read the status, because reading the received char
498 * destroys the status of this char.
499 */
500 rr1 = zs_read_reg(cs, 1);
501 c = zs_read_data(cs);
502
503 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
504 /* Clear the receive error. */
505 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
506 }
507
508 ms->ms_rbuf[put] = (c << 8) | rr1;
509 put_next = (put + 1) & MS_RX_RING_MASK;
510
511 /* Would overrun if increment makes (put==get). */
512 if (put_next == ms->ms_rbget) {
513 ms->ms_intr_flags |= INTR_RX_OVERRUN;
514 } else {
515 /* OK, really increment. */
516 put = put_next;
517 }
518
519 /* Done reading. */
520 ms->ms_rbput = put;
521
522 /* Ask for softint() call. */
523 cs->cs_softreq = 1;
524 }
525
526
527 static void
528 ms_txint(cs)
529 register struct zs_chanstate *cs;
530 {
531 register struct ms_softc *ms;
532
533 ms = cs->cs_private;
534 zs_write_csr(cs, ZSWR0_RESET_TXINT);
535 ms->ms_intr_flags |= INTR_TX_EMPTY;
536 /* Ask for softint() call. */
537 cs->cs_softreq = 1;
538 }
539
540
541 static void
542 ms_stint(cs, force)
543 register struct zs_chanstate *cs;
544 int force;
545 {
546 register struct ms_softc *ms;
547 register int rr0;
548
549 ms = cs->cs_private;
550
551 rr0 = zs_read_csr(cs);
552 zs_write_csr(cs, ZSWR0_RESET_STATUS);
553
554 /*
555 * We have to accumulate status line changes here.
556 * Otherwise, if we get multiple status interrupts
557 * before the softint runs, we could fail to notice
558 * some status line changes in the softint routine.
559 * Fix from Bill Studenmund, October 1996.
560 */
561 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
562 cs->cs_rr0 = rr0;
563 ms->ms_intr_flags |= INTR_ST_CHECK;
564
565 /* Ask for softint() call. */
566 cs->cs_softreq = 1;
567 }
568
569
570 static void
571 ms_softint(cs)
572 struct zs_chanstate *cs;
573 {
574 register struct ms_softc *ms;
575 register int get, c, s;
576 int intr_flags;
577 register u_short ring_data;
578
579 ms = cs->cs_private;
580
581 /* Atomically get and clear flags. */
582 s = splzs();
583 intr_flags = ms->ms_intr_flags;
584 ms->ms_intr_flags = 0;
585
586 /* Now lower to spltty for the rest. */
587 (void) spltty();
588
589 /*
590 * Copy data from the receive ring to the event layer.
591 */
592 get = ms->ms_rbget;
593 while (get != ms->ms_rbput) {
594 ring_data = ms->ms_rbuf[get];
595 get = (get + 1) & MS_RX_RING_MASK;
596
597 /* low byte of ring_data is rr1 */
598 c = (ring_data >> 8) & 0xff;
599
600 if (ring_data & ZSRR1_DO)
601 intr_flags |= INTR_RX_OVERRUN;
602 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
603 log(LOG_ERR, "%s: input error (0x%x)\n",
604 ms->ms_dev.dv_xname, ring_data);
605 c = -1; /* signal input error */
606 }
607
608 /* Pass this up to the "middle" layer. */
609 ms_input(ms, c);
610 }
611 if (intr_flags & INTR_RX_OVERRUN) {
612 log(LOG_ERR, "%s: input overrun\n",
613 ms->ms_dev.dv_xname);
614 }
615 ms->ms_rbget = get;
616
617 if (intr_flags & INTR_TX_EMPTY) {
618 /*
619 * Transmit done. (Not expected.)
620 */
621 log(LOG_ERR, "%s: transmit interrupt?\n",
622 ms->ms_dev.dv_xname);
623 }
624
625 if (intr_flags & INTR_ST_CHECK) {
626 /*
627 * Status line change. (Not expected.)
628 */
629 log(LOG_ERR, "%s: status interrupt?\n",
630 ms->ms_dev.dv_xname);
631 cs->cs_rr0_delta = 0;
632 }
633
634 splx(s);
635 }
636
637 struct zsops zsops_ms = {
638 ms_rxint, /* receive char available */
639 ms_stint, /* external/status */
640 ms_txint, /* xmit buffer empty */
641 ms_softint, /* process software interrupt */
642 };
643
644
645 static void
646 ms_trigger (cs, onoff)
647 struct zs_chanstate *cs;
648 int onoff;
649 {
650 /* for front connected one */
651 if (onoff)
652 cs->cs_preg[5] |= ZSWR5_RTS;
653 else
654 cs->cs_preg[5] &= ~ZSWR5_RTS;
655 cs->cs_creg[5] = cs->cs_preg[5];
656 zs_write_reg(cs, 5, cs->cs_preg[5]);
657
658 /* for keyborad connected one */
659 mfp_send_usart (onoff | 0x40);
660 }
661
662 /*
663 * mouse timer interrupt.
664 * called after system tick interrupt is done.
665 */
666 void
667 ms_modem(arg)
668 void *arg;
669 {
670 struct ms_softc *ms = arg;
671 int s;
672
673 if (!ms->ms_ready)
674 return;
675
676 s = splzs();
677
678 if (ms->ms_nodata++ > 250) { /* XXX */
679 log(LOG_ERR, "%s: no data for 5 secs. resetting.\n",
680 ms->ms_dev.dv_xname);
681 ms->ms_byteno = -1;
682 ms->ms_nodata = 0;
683 ms->ms_rts = 0;
684 }
685
686 if (ms->ms_rts) {
687 if (ms->ms_byteno == -1) {
688 /* start next sequence */
689 ms->ms_rts = 0;
690 ms_trigger(ms->ms_cs, ms->ms_rts);
691 ms->ms_byteno = 0;
692 }
693 } else {
694 ms->ms_rts = 1;
695 ms_trigger(ms->ms_cs, ms->ms_rts);
696 }
697
698 (void) splx(s);
699 callout_reset(&ms->ms_modem_ch, 2, ms_modem, ms);
700 }
701