Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / x68k / dev / ms.c
blob33f8f715dd8601e3ab1c840b8f13cdafc7000ddc
1 /* $NetBSD: ms.c,v 1.29 2008/06/25 08:19:37 isaki Exp $ */
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.
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.
40 * @(#)ms.c 8.1 (Berkeley) 6/11/93
44 * X68k mouse driver.
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.29 2008/06/25 08:19:37 isaki Exp $");
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>
61 #include <dev/ic/z8530reg.h>
62 #include <machine/z8530var.h>
64 #include <arch/x68k/dev/event_var.h>
65 #include <machine/vuid_event.h>
66 #include <arch/x68k/dev/mfp.h>
68 #include "ioconf.h"
69 #include "locators.h"
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!
76 #define MS_RX_RING_SIZE 256
77 #define MS_RX_RING_MASK (MS_RX_RING_SIZE-1)
79 * Output buffer. Only need a few chars.
81 #define MS_TX_RING_SIZE 16
82 #define MS_TX_RING_MASK (MS_TX_RING_SIZE-1)
84 * Mouse serial line is fixed at 4800 bps.
86 #define MS_BPS 4800
89 * Mouse state. A SHARP X1/X680x0 mouse is a fairly simple device,
90 * producing three-byte blobs of the form:
92 * b dx dy
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.
97 * It needs a trigger for the transmission. When zs RTS negated, the
98 * mouse begins the sequence. RTS assertion has no effect.
100 struct ms_softc {
101 device_t ms_dev; /* required first: base device */
102 struct zs_chanstate *ms_cs;
104 struct callout ms_modem_ch;
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
113 * The receive ring buffer.
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 */
120 * State of input translator
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;
131 * State of upper interface.
133 volatile int ms_ready; /* event queue is ready */
134 struct evvar ms_events; /* event queue state */
135 } ms_softc;
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 *);
142 CFATTACH_DECL_NEW(ms, sizeof(struct ms_softc),
143 ms_match, ms_attach, NULL, NULL);
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);
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 */
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);
165 const struct cdevsw ms_cdevsw ={
166 msopen, msclose, msread, nowrite, msioctl,
167 nostop, notty, mspoll, nommap, mskqfilter,
171 * ms_match: how is this zs channel configured?
174 ms_match(device_t parent, cfdata_t cf, void *aux)
176 struct zsc_attach_args *args = aux;
177 struct zsc_softc *zsc = device_private(parent);
179 /* Exact match required for the mouse. */
180 if (cf->cf_loc[ZSCCF_CHANNEL] != args->channel)
181 return 0;
182 if (args->channel != 1)
183 return 0;
184 if (&zsc->zsc_addr->zs_chan_b != (struct zschan *)ZSMS_PHYSADDR)
185 return 0;
187 return 2;
190 void
191 ms_attach(device_t parent, device_t self, void *aux)
193 struct ms_softc *ms = device_private(self);
194 struct zsc_softc *zsc = device_private(parent);
195 struct zs_chanstate *cs;
196 cfdata_t cf;
197 int reset, s;
199 ms->ms_dev = self;
200 callout_init(&ms->ms_modem_ch, 0);
202 cf = device_cfdata(self);
203 cs = zsc->zsc_cs[1];
204 cs->cs_private = ms;
205 cs->cs_ops = &zsops_ms;
206 ms->ms_cs = cs;
208 /* Initialize the speed, etc. */
209 s = splzs();
210 /* May need reset... */
211 reset = ZSWR9_B_RESET;
212 zs_write_reg(cs, 9, reset);
213 /* We don't care about status or tx interrupts. */
214 cs->cs_preg[1] = ZSWR1_RIE;
215 cs->cs_preg[4] = ZSWR4_CLK_X16 | ZSWR4_TWOSB;
216 (void)zs_set_speed(cs, MS_BPS);
217 zs_loadchannelregs(cs);
218 splx(s);
220 /* Initialize translator. */
221 ms->ms_ready = 0;
223 aprint_normal("\n");
226 /****************************************************************
227 * Entry points for /dev/mouse
228 * (open,close,read,write,...)
229 ****************************************************************/
232 msopen(dev_t dev, int flags, int mode, struct lwp *l)
234 struct ms_softc *ms;
236 ms = device_lookup_private(&ms_cd, minor(dev));
237 if (ms == NULL)
238 return ENXIO;
240 /* This is an exclusive open device. */
241 if (ms->ms_events.ev_io)
242 return EBUSY;
243 ms->ms_events.ev_io = l->l_proc;
244 ev_init(&ms->ms_events); /* may cause sleep */
246 ms->ms_ready = 1; /* start accepting events */
247 ms->ms_rts = 1;
248 ms->ms_byteno = -1;
249 ms->ms_nodata = 0;
251 /* start sequencer */
252 ms_modem(ms);
254 return 0;
258 msclose(dev_t dev, int flags, int mode, struct lwp *l)
260 struct ms_softc *ms;
262 ms = device_lookup_private(&ms_cd, minor(dev));
263 ms->ms_ready = 0; /* stop accepting events */
264 callout_stop(&ms->ms_modem_ch);
265 ev_fini(&ms->ms_events);
267 ms->ms_events.ev_io = NULL;
268 return 0;
272 msread(dev_t dev, struct uio *uio, int flags)
274 struct ms_softc *ms;
276 ms = device_lookup_private(&ms_cd, minor(dev));
277 return ev_read(&ms->ms_events, uio, flags);
281 msioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
283 struct ms_softc *ms;
285 ms = device_lookup_private(&ms_cd, minor(dev));
287 switch (cmd) {
289 case FIONBIO: /* we will remove this someday (soon???) */
290 return 0;
292 case FIOASYNC:
293 ms->ms_events.ev_async = *(int *)data != 0;
294 return 0;
296 case FIOSETOWN:
297 if (-*(int *)data != ms->ms_events.ev_io->p_pgid
298 && *(int *)data != ms->ms_events.ev_io->p_pid)
299 return EPERM;
300 return 0;
302 case TIOCSPGRP:
303 if (*(int *)data != ms->ms_events.ev_io->p_pgid)
304 return EPERM;
305 return 0;
307 case VUIDGFORMAT:
308 /* we only do firm_events */
309 *(int *)data = VUID_FIRM_EVENT;
310 return 0;
312 case VUIDSFORMAT:
313 if (*(int *)data != VUID_FIRM_EVENT)
314 return EINVAL;
315 return 0;
317 return ENOTTY;
321 mspoll(dev_t dev, int events, struct lwp *l)
323 struct ms_softc *ms;
325 ms = device_lookup_private(&ms_cd, minor(dev));
326 return ev_poll(&ms->ms_events, events, l);
330 mskqfilter(dev_t dev, struct knote *kn)
332 struct ms_softc *ms;
334 ms = device_lookup_private(&ms_cd, minor(dev));
335 return ev_kqfilter(&ms->ms_events, kn);
338 /****************************************************************
339 * Middle layer (translator)
340 ****************************************************************/
343 * Called by our ms_softint() routine on input.
345 static void
346 ms_input(struct ms_softc *ms, int c)
348 struct firm_event *fe;
349 int mb, ub, d, get, put, any;
350 static const char to_one[] = { 1, 2, 3 };
351 static const int to_id[] = { MS_LEFT, MS_RIGHT, MS_MIDDLE };
354 * Discard input if not ready. Drop sync on parity or framing
355 * error; gain sync on button byte.
357 if (ms->ms_ready == 0)
358 return;
360 ms->ms_nodata = 0;
362 * Run the decode loop, adding to the current information.
363 * We add, rather than replace, deltas, so that if the event queue
364 * fills, we accumulate data for when it opens up again.
366 switch (ms->ms_byteno) {
368 case -1:
369 return;
371 case 0:
372 /* buttons */
373 ms->ms_byteno = 1;
374 ms->ms_mb = c & 0x3;
375 return;
377 case 1:
378 /* delta-x */
379 ms->ms_byteno = 2;
380 ms->ms_dx += (char)c;
381 return;
383 case 2:
384 /* delta-y */
385 ms->ms_byteno = -1;
386 ms->ms_dy += (char)c;
387 break;
389 default:
390 panic("ms_input");
391 /* NOTREACHED */
395 * We have at least one event (mouse button, delta-X, or
396 * delta-Y; possibly all three, and possibly three separate
397 * button events). Deliver these events until we are out
398 * of changes or out of room. As events get delivered,
399 * mark them `unchanged'.
401 any = 0;
402 get = ms->ms_events.ev_get;
403 put = ms->ms_events.ev_put;
404 fe = &ms->ms_events.ev_q[put];
406 /* NEXT prepares to put the next event, backing off if necessary */
407 #define NEXT \
408 if ((++put) % EV_QSIZE == get) { \
409 put--; \
410 goto out; \
412 /* ADVANCE completes the `put' of the event */
413 #define ADVANCE \
414 fe++; \
415 if (put >= EV_QSIZE) { \
416 put = 0; \
417 fe = &ms->ms_events.ev_q[0]; \
420 mb = ms->ms_mb;
421 ub = ms->ms_ub;
422 while ((d = mb ^ ub) != 0) {
424 * Mouse button change. Convert up to three changes
425 * to the `first' change, and drop it into the event queue.
427 NEXT;
428 d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
429 fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
430 fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
431 firm_gettime(fe);
432 ADVANCE;
433 ub ^= d;
434 any++;
436 if (ms->ms_dx) {
437 NEXT;
438 fe->id = LOC_X_DELTA;
439 fe->value = ms->ms_dx;
440 firm_gettime(fe);
441 ADVANCE;
442 ms->ms_dx = 0;
443 any++;
445 if (ms->ms_dy) {
446 NEXT;
447 fe->id = LOC_Y_DELTA;
448 fe->value = -ms->ms_dy; /* XXX? */
449 firm_gettime(fe);
450 ADVANCE;
451 ms->ms_dy = 0;
452 any++;
454 out:
455 if (any) {
456 ms->ms_ub = ub;
457 ms->ms_events.ev_put = put;
458 EV_WAKEUP(&ms->ms_events);
462 /****************************************************************
463 * Interface to the lower layer (zscc)
464 ****************************************************************/
466 static void
467 ms_rxint(struct zs_chanstate *cs)
469 struct ms_softc *ms;
470 int put, put_next;
471 u_char c, rr1;
473 ms = cs->cs_private;
474 put = ms->ms_rbput;
477 * First read the status, because reading the received char
478 * destroys the status of this char.
480 rr1 = zs_read_reg(cs, 1);
481 c = zs_read_data(cs);
483 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
484 /* Clear the receive error. */
485 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
488 ms->ms_rbuf[put] = (c << 8) | rr1;
489 put_next = (put + 1) & MS_RX_RING_MASK;
491 /* Would overrun if increment makes (put==get). */
492 if (put_next == ms->ms_rbget) {
493 ms->ms_intr_flags |= INTR_RX_OVERRUN;
494 } else {
495 /* OK, really increment. */
496 put = put_next;
499 /* Done reading. */
500 ms->ms_rbput = put;
502 /* Ask for softint() call. */
503 cs->cs_softreq = 1;
507 static void
508 ms_txint(struct zs_chanstate *cs)
510 struct ms_softc *ms;
512 ms = cs->cs_private;
513 zs_write_csr(cs, ZSWR0_RESET_TXINT);
514 ms->ms_intr_flags |= INTR_TX_EMPTY;
515 /* Ask for softint() call. */
516 cs->cs_softreq = 1;
520 static void
521 ms_stint(struct zs_chanstate *cs, int force)
523 struct ms_softc *ms;
524 int rr0;
526 ms = cs->cs_private;
528 rr0 = zs_read_csr(cs);
529 zs_write_csr(cs, ZSWR0_RESET_STATUS);
532 * We have to accumulate status line changes here.
533 * Otherwise, if we get multiple status interrupts
534 * before the softint runs, we could fail to notice
535 * some status line changes in the softint routine.
536 * Fix from Bill Studenmund, October 1996.
538 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
539 cs->cs_rr0 = rr0;
540 ms->ms_intr_flags |= INTR_ST_CHECK;
542 /* Ask for softint() call. */
543 cs->cs_softreq = 1;
547 static void
548 ms_softint(struct zs_chanstate *cs)
550 struct ms_softc *ms;
551 int get, c, s;
552 int intr_flags;
553 u_short ring_data;
555 ms = cs->cs_private;
557 /* Atomically get and clear flags. */
558 s = splzs();
559 intr_flags = ms->ms_intr_flags;
560 ms->ms_intr_flags = 0;
562 /* Now lower to spltty for the rest. */
563 (void) spltty();
566 * Copy data from the receive ring to the event layer.
568 get = ms->ms_rbget;
569 while (get != ms->ms_rbput) {
570 ring_data = ms->ms_rbuf[get];
571 get = (get + 1) & MS_RX_RING_MASK;
573 /* low byte of ring_data is rr1 */
574 c = (ring_data >> 8) & 0xff;
576 if (ring_data & ZSRR1_DO)
577 intr_flags |= INTR_RX_OVERRUN;
578 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
579 log(LOG_ERR, "%s: input error (0x%x)\n",
580 device_xname(ms->ms_dev), ring_data);
581 c = -1; /* signal input error */
584 /* Pass this up to the "middle" layer. */
585 ms_input(ms, c);
587 if (intr_flags & INTR_RX_OVERRUN) {
588 log(LOG_ERR, "%s: input overrun\n",
589 device_xname(ms->ms_dev));
591 ms->ms_rbget = get;
593 if (intr_flags & INTR_TX_EMPTY) {
595 * Transmit done. (Not expected.)
597 log(LOG_ERR, "%s: transmit interrupt?\n",
598 device_xname(ms->ms_dev));
601 if (intr_flags & INTR_ST_CHECK) {
603 * Status line change. (Not expected.)
605 log(LOG_ERR, "%s: status interrupt?\n",
606 device_xname(ms->ms_dev));
607 cs->cs_rr0_delta = 0;
610 splx(s);
614 static void
615 ms_trigger(struct zs_chanstate *cs, int onoff)
617 /* for front connected one */
618 if (onoff)
619 cs->cs_preg[5] |= ZSWR5_RTS;
620 else
621 cs->cs_preg[5] &= ~ZSWR5_RTS;
622 cs->cs_creg[5] = cs->cs_preg[5];
623 zs_write_reg(cs, 5, cs->cs_preg[5]);
625 /* for keyborad connected one */
626 mfp_send_usart(onoff | 0x40);
630 * mouse timer interrupt.
631 * called after system tick interrupt is done.
633 void
634 ms_modem(void *arg)
636 struct ms_softc *ms = arg;
637 int s;
639 if (!ms->ms_ready)
640 return;
642 s = splzs();
644 if (ms->ms_nodata++ > 250) { /* XXX */
645 log(LOG_ERR, "%s: no data for 5 secs. resetting.\n",
646 device_xname(ms->ms_dev));
647 ms->ms_byteno = -1;
648 ms->ms_nodata = 0;
649 ms->ms_rts = 0;
652 if (ms->ms_rts) {
653 if (ms->ms_byteno == -1) {
654 /* start next sequence */
655 ms->ms_rts = 0;
656 ms_trigger(ms->ms_cs, ms->ms_rts);
657 ms->ms_byteno = 0;
659 } else {
660 ms->ms_rts = 1;
661 ms_trigger(ms->ms_cs, ms->ms_rts);
664 (void) splx(s);
665 callout_reset(&ms->ms_modem_ch, 2, ms_modem, ms);