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