ms_zs.c revision 1.1 1 /* $NetBSD: ms_zs.c,v 1.1 1999/05/14 06:42:02 mrg 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 * Mouse driver (/dev/mouse)
49 */
50
51 /*
52 * Zilog Z8530 Dual UART driver (mouse interface)
53 *
54 * This is the "slave" driver that will be attached to
55 * the "zsc" driver for a Sun mouse.
56 */
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/conf.h>
61 #include <sys/device.h>
62 #include <sys/ioctl.h>
63 #include <sys/kernel.h>
64 #include <sys/proc.h>
65 #include <sys/signal.h>
66 #include <sys/signalvar.h>
67 #include <sys/time.h>
68 #include <sys/select.h>
69 #include <sys/syslog.h>
70
71 #include <machine/vuid_event.h>
72
73 #include <dev/ic/z8530reg.h>
74 #include <machine/z8530var.h>
75 #include <dev/sun/event_var.h>
76 #include <dev/sun/msvar.h>
77
78 static void ms_zs_rxint __P((struct zs_chanstate *));
79 static void ms_zs_stint __P((struct zs_chanstate *, int));
80 static void ms_zs_txint __P((struct zs_chanstate *));
81 static void ms_zs_softint __P((struct zs_chanstate *));
82
83 struct zsops zsops_ms = {
84 ms_zs_rxint, /* receive char available */
85 ms_zs_stint, /* external/status */
86 ms_zs_txint, /* xmit buffer empty */
87 ms_zs_softint, /* process software interrupt */
88 };
89
90 static int ms_zs_match(struct device *, struct cfdata *, void *);
91 static void ms_zs_attach(struct device *, struct device *, void *);
92
93 struct cfattach ms_zs_ca = {
94 sizeof(struct ms_softc), ms_zs_match, ms_zs_attach
95 };
96
97 /*
98 * ms_match: how is this zs channel configured?
99 */
100 int
101 ms_zs_match(parent, cf, aux)
102 struct device *parent;
103 struct cfdata *cf;
104 void *aux;
105 {
106 struct zsc_attach_args *args = aux;
107
108 /* Exact match required for keyboard. */
109 if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
110 return 2;
111
112 return 0;
113 }
114
115 void
116 ms_zs_attach(parent, self, aux)
117 struct device *parent, *self;
118 void *aux;
119
120 {
121 struct zsc_softc *zsc = (void *) parent;
122 struct ms_softc *ms = (void *) self;
123 struct zsc_attach_args *args = aux;
124 struct zs_chanstate *cs;
125 struct cfdata *cf;
126 int channel, ms_unit;
127 int reset, s;
128
129 cf = ms->ms_dev.dv_cfdata;
130 ms_unit = ms->ms_dev.dv_unit;
131 channel = args->channel;
132 cs = zsc->zsc_cs[channel];
133 cs->cs_private = ms;
134 cs->cs_ops = &zsops_ms;
135 ms->ms_cs = cs;
136
137 printf("\n");
138
139 /* Initialize the speed, etc. */
140 s = splzs();
141 /* May need reset... */
142 reset = (channel == 0) ?
143 ZSWR9_A_RESET : ZSWR9_B_RESET;
144 zs_write_reg(cs, 9, reset);
145 /* These are OK as set by zscc: WR3, WR4, WR5 */
146 /* We don't care about status or tx interrupts. */
147 cs->cs_preg[1] = ZSWR1_RIE;
148 (void) zs_set_speed(cs, MS_BPS);
149 zs_loadchannelregs(cs);
150 splx(s);
151
152 /* Initialize translator. */
153 ms->ms_byteno = -1;
154 }
155
156 /****************************************************************
157 * Interface to the lower layer (zscc)
158 ****************************************************************/
159
160 static void
161 ms_zs_rxint(cs)
162 register struct zs_chanstate *cs;
163 {
164 register struct ms_softc *ms;
165 register int put, put_next;
166 register u_char c, rr1;
167
168 ms = cs->cs_private;
169 put = ms->ms_rbput;
170
171 /*
172 * First read the status, because reading the received char
173 * destroys the status of this char.
174 */
175 rr1 = zs_read_reg(cs, 1);
176 c = zs_read_data(cs);
177
178 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
179 /* Clear the receive error. */
180 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
181 }
182
183 ms->ms_rbuf[put] = (c << 8) | rr1;
184 put_next = (put + 1) & MS_RX_RING_MASK;
185
186 /* Would overrun if increment makes (put==get). */
187 if (put_next == ms->ms_rbget) {
188 ms->ms_intr_flags |= INTR_RX_OVERRUN;
189 } else {
190 /* OK, really increment. */
191 put = put_next;
192 }
193
194 /* Done reading. */
195 ms->ms_rbput = put;
196
197 /* Ask for softint() call. */
198 cs->cs_softreq = 1;
199 }
200
201 static void
202 ms_zs_txint(cs)
203 register struct zs_chanstate *cs;
204 {
205 register struct ms_softc *ms;
206
207 ms = cs->cs_private;
208 zs_write_csr(cs, ZSWR0_RESET_TXINT);
209 ms->ms_intr_flags |= INTR_TX_EMPTY;
210 /* Ask for softint() call. */
211 cs->cs_softreq = 1;
212 }
213
214 static void
215 ms_zs_stint(cs, force)
216 register struct zs_chanstate *cs;
217 int force;
218 {
219 register struct ms_softc *ms;
220 register int rr0;
221
222 ms = cs->cs_private;
223
224 rr0 = zs_read_csr(cs);
225 zs_write_csr(cs, ZSWR0_RESET_STATUS);
226
227 /*
228 * We have to accumulate status line changes here.
229 * Otherwise, if we get multiple status interrupts
230 * before the softint runs, we could fail to notice
231 * some status line changes in the softint routine.
232 * Fix from Bill Studenmund, October 1996.
233 */
234 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
235 cs->cs_rr0 = rr0;
236 ms->ms_intr_flags |= INTR_ST_CHECK;
237
238 /* Ask for softint() call. */
239 cs->cs_softreq = 1;
240 }
241
242 static void
243 ms_zs_softint(cs)
244 struct zs_chanstate *cs;
245 {
246 register struct ms_softc *ms;
247 register int get, c, s;
248 int intr_flags;
249 register u_short ring_data;
250
251 ms = cs->cs_private;
252
253 /* Atomically get and clear flags. */
254 s = splzs();
255 intr_flags = ms->ms_intr_flags;
256 ms->ms_intr_flags = 0;
257
258 /* Now lower to spltty for the rest. */
259 (void) spltty();
260
261 /*
262 * Copy data from the receive ring to the event layer.
263 */
264 get = ms->ms_rbget;
265 while (get != ms->ms_rbput) {
266 ring_data = ms->ms_rbuf[get];
267 get = (get + 1) & MS_RX_RING_MASK;
268
269 /* low byte of ring_data is rr1 */
270 c = (ring_data >> 8) & 0xff;
271
272 if (ring_data & ZSRR1_DO)
273 intr_flags |= INTR_RX_OVERRUN;
274 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
275 log(LOG_ERR, "%s: input error (0x%x)\n",
276 ms->ms_dev.dv_xname, ring_data);
277 c = -1; /* signal input error */
278 }
279
280 /* Pass this up to the "middle" layer. */
281 ms_input(ms, c);
282 }
283 if (intr_flags & INTR_RX_OVERRUN) {
284 log(LOG_ERR, "%s: input overrun\n",
285 ms->ms_dev.dv_xname);
286 }
287 ms->ms_rbget = get;
288
289 if (intr_flags & INTR_TX_EMPTY) {
290 /*
291 * Transmit done. (Not expected.)
292 */
293 log(LOG_ERR, "%s: transmit interrupt?\n",
294 ms->ms_dev.dv_xname);
295 }
296
297 if (intr_flags & INTR_ST_CHECK) {
298 /*
299 * Status line change. (Not expected.)
300 */
301 log(LOG_ERR, "%s: status interrupt?\n",
302 ms->ms_dev.dv_xname);
303 cs->cs_rr0_delta = 0;
304 }
305
306 splx(s);
307 }
308