zsms.c revision 1.3 1 /* $NetBSD: zsms.c,v 1.3 2000/10/19 10:27:04 nisimura 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 * VSXXX mice attached with channel A of the 1st SCC
49 */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/ioctl.h>
55 #include <sys/syslog.h>
56 #include <sys/kernel.h>
57 #include <sys/proc.h>
58 #include <sys/tty.h>
59
60 #include <dev/ic/z8530reg.h>
61 #include <machine/z8530var.h>
62
63 #include <dev/dec/lk201.h>
64
65 #include <dev/wscons/wsconsio.h>
66 #include <dev/wscons/wsmousevar.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 ZSMS_RX_RING_SIZE 256
76 #define ZSMS_RX_RING_MASK (ZSMS_RX_RING_SIZE-1)
77 /*
78 * Output buffer. Only need a few chars.
79 */
80 #define ZSMS_TX_RING_SIZE 16
81 #define ZSMS_TX_RING_MASK (ZSMS_TX_RING_SIZE-1)
82
83 #define ZSMS_BPS 4800
84
85 struct zsms_softc { /* driver status information */
86 struct device zsms_dev; /* required first: base device */
87 struct zs_chanstate *zsms_cs;
88
89 /* Flags to communicate with zsms_softintr() */
90 volatile int zsms_intr_flags;
91 #define INTR_RX_OVERRUN 1
92 #define INTR_TX_EMPTY 2
93 #define INTR_ST_CHECK 4
94
95 /*
96 * The receive ring buffer.
97 */
98 u_int zsms_rbget; /* ring buffer `get' index */
99 volatile u_int zsms_rbput; /* ring buffer `put' index */
100 u_short zsms_rbuf[ZSMS_RX_RING_SIZE]; /* rr1, data pairs */
101
102 int sc_enabled; /* input enabled? */
103 int sc_selftest; /* self test in progress */
104
105 int inputstate;
106 u_int buttons;
107 int dx, dy;
108
109 struct device *sc_wsmousedev;
110 };
111
112 struct zsops zsops_zsms;
113
114 static int zsms_match __P((struct device *, struct cfdata *, void *));
115 static void zsms_attach __P((struct device *, struct device *, void *));
116 static void zsms_input __P((void *, int));
117
118 struct cfattach zsms_ca = {
119 sizeof(struct zsms_softc), zsms_match, zsms_attach,
120 };
121
122 static int zsms_enable __P((void *));
123 static int zsms_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
124 static void zsms_disable __P((void *));
125
126 const struct wsmouse_accessops zsms_accessops = {
127 zsms_enable,
128 zsms_ioctl,
129 zsms_disable,
130 };
131
132 static int
133 zsms_match(parent, cf, aux)
134 struct device *parent;
135 struct cfdata *cf;
136 void *aux;
137 {
138 struct zsc_attach_args *args = aux;
139
140 /* Exact match is better than wildcard. */
141 if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
142 return 2;
143
144 /* This driver accepts wildcard. */
145 if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
146 return 1;
147
148 return 0;
149 }
150
151 static void
152 zsms_attach(parent, self, aux)
153 struct device *parent, *self;
154 void *aux;
155 {
156 struct zsc_softc *zsc = (void *)parent;
157 struct zsms_softc *zsms = (void *)self;
158 struct zsc_attach_args *args = aux;
159 struct zs_chanstate *cs;
160 struct wsmousedev_attach_args a;
161 int s;
162
163 cs = zsc->zsc_cs[args->channel];
164 cs->cs_private = zsms;
165 cs->cs_ops = &zsops_zsms;
166 zsms->zsms_cs = cs;
167
168 printf("\n");
169
170 /* Initialize the speed, etc. */
171 s = splzs();
172 /* May need reset... */
173 zs_write_reg(cs, 9, ZSWR9_A_RESET);
174 /* These are OK as set by zscc: WR3, WR5 */
175 /* We don't care about status or tx interrupts. */
176 cs->cs_preg[1] = ZSWR1_RIE;
177 (void) zs_set_speed(cs, ZSMS_BPS);
178
179 /* mouse wants odd parity */
180 cs->cs_preg[4] |= ZSWR4_PARENB;
181 /* cs->cs_preg[4] &= ~ZSWR4_EVENP; (no-op) */
182
183 zs_loadchannelregs(cs);
184 splx(s);
185
186 a.accessops = &zsms_accessops;
187 a.accesscookie = zsms;
188
189 zsms->sc_enabled = 0;
190 zsms->sc_selftest = 0;
191 zsms->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
192 }
193
194 static int
195 zsms_enable(v)
196 void *v;
197 {
198 struct zsms_softc *sc = v;
199
200 if (sc->sc_enabled)
201 return EBUSY;
202
203 sc->sc_selftest = 4; /* wait for 4 byte reply upto 1/2 sec */
204 zs_write_data(sc->zsms_cs, MOUSE_SELF_TEST);
205 (void)tsleep(zsms_enable, TTIPRI, "zsmsopen", hz / 2);
206 if (sc->sc_selftest != 0) {
207 sc->sc_selftest = 0;
208 return ENXIO;
209 }
210 /* XXX DELAY before mode set? */
211 zs_write_data(sc->zsms_cs, MOUSE_INCREMENTAL);
212 sc->sc_enabled = 1;
213 sc->inputstate = 0;
214 return 0;
215 }
216
217 static void
218 zsms_disable(v)
219 void *v;
220 {
221 struct zsms_softc *sc = v;
222
223 sc->sc_enabled = 0;
224 }
225
226 static int
227 zsms_ioctl(v, cmd, data, flag, p)
228 void *v;
229 u_long cmd;
230 caddr_t data;
231 int flag;
232 struct proc *p;
233 {
234
235 if (cmd == WSMOUSEIO_GTYPE) {
236 *(u_int *)data = WSMOUSE_TYPE_VSXXX;
237 return 0;
238 }
239 return -1;
240 }
241
242 static void
243 zsms_input(vsc, data)
244 void *vsc;
245 int data;
246 {
247 struct zsms_softc *sc = vsc;
248
249 if (sc->sc_enabled == 0) {
250 if (sc->sc_selftest > 0) {
251 sc->sc_selftest -= 1;
252 if (sc->sc_selftest == 0)
253 wakeup(zsms_enable);
254 }
255 return;
256 }
257
258 if ((data & MOUSE_START_FRAME) != 0)
259 sc->inputstate = 1;
260 else
261 sc->inputstate++;
262
263 if (sc->inputstate == 1) {
264 /* LMR -> RML: wsevents counts 0 for the left-most */
265 sc->buttons = data & 02;
266 if (data & 01)
267 sc->buttons |= 04;
268 if (data & 04)
269 sc->buttons |= 01;
270 sc->dx = data & MOUSE_X_SIGN;
271 sc->dy = data & MOUSE_Y_SIGN;
272 } else if (sc->inputstate == 2) {
273 if (sc->dx == 0)
274 sc->dx = -data;
275 else
276 sc->dx = data;
277 } else if (sc->inputstate == 3) {
278 sc->inputstate = 0;
279 if (sc->dy == 0)
280 sc->dy = -data;
281 else
282 sc->dy = data;
283 wsmouse_input(sc->sc_wsmousedev, sc->buttons,
284 sc->dx, sc->dy, 0, WSMOUSE_INPUT_DELTA);
285 }
286
287 return;
288 }
289
290 /****************************************************************
291 * Interface to the lower layer (zscc)
292 ****************************************************************/
293
294 static void zsms_rxint __P((struct zs_chanstate *));
295 static void zsms_stint __P((struct zs_chanstate *, int));
296 static void zsms_txint __P((struct zs_chanstate *));
297 static void zsms_softint __P((struct zs_chanstate *));
298
299 static void
300 zsms_rxint(cs)
301 struct zs_chanstate *cs;
302 {
303 struct zsms_softc *zsms;
304 int put, put_next;
305 u_char c, rr1;
306
307 zsms = cs->cs_private;
308 put = zsms->zsms_rbput;
309
310 /*
311 * First read the status, because reading the received char
312 * destroys the status of this char.
313 */
314 rr1 = zs_read_reg(cs, 1);
315 c = zs_read_data(cs);
316 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
317 /* Clear the receive error. */
318 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
319 }
320
321 zsms->zsms_rbuf[put] = (c << 8) | rr1;
322 put_next = (put + 1) & ZSMS_RX_RING_MASK;
323
324 /* Would overrun if increment makes (put==get). */
325 if (put_next == zsms->zsms_rbget) {
326 zsms->zsms_intr_flags |= INTR_RX_OVERRUN;
327 } else {
328 /* OK, really increment. */
329 put = put_next;
330 }
331
332 /* Done reading. */
333 zsms->zsms_rbput = put;
334
335 /* Ask for softint() call. */
336 cs->cs_softreq = 1;
337 }
338
339
340 static void
341 zsms_txint(cs)
342 struct zs_chanstate *cs;
343 {
344 struct zsms_softc *zsms;
345
346 zsms = cs->cs_private;
347 zs_write_csr(cs, ZSWR0_RESET_TXINT);
348 zsms->zsms_intr_flags |= INTR_TX_EMPTY;
349 /* Ask for softint() call. */
350 cs->cs_softreq = 1;
351 }
352
353
354 static void
355 zsms_stint(cs, force)
356 struct zs_chanstate *cs;
357 int force;
358 {
359 struct zsms_softc *zsms;
360 int rr0;
361
362 zsms = cs->cs_private;
363
364 rr0 = zs_read_csr(cs);
365 zs_write_csr(cs, ZSWR0_RESET_STATUS);
366
367 /*
368 * We have to accumulate status line changes here.
369 * Otherwise, if we get multiple status interrupts
370 * before the softint runs, we could fail to notice
371 * some status line changes in the softint routine.
372 * Fix from Bill Studenmund, October 1996.
373 */
374 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
375 cs->cs_rr0 = rr0;
376 zsms->zsms_intr_flags |= INTR_ST_CHECK;
377
378 /* Ask for softint() call. */
379 cs->cs_softreq = 1;
380 }
381
382
383 static void
384 zsms_softint(cs)
385 struct zs_chanstate *cs;
386 {
387 struct zsms_softc *zsms;
388 int get, c, s;
389 int intr_flags;
390 u_short ring_data;
391
392 zsms = cs->cs_private;
393
394 /* Atomically get and clear flags. */
395 s = splzs();
396 intr_flags = zsms->zsms_intr_flags;
397 zsms->zsms_intr_flags = 0;
398
399 /* Now lower to spltty for the rest. */
400 (void) spltty();
401
402 /*
403 * Copy data from the receive ring to the event layer.
404 */
405 get = zsms->zsms_rbget;
406 while (get != zsms->zsms_rbput) {
407 ring_data = zsms->zsms_rbuf[get];
408 get = (get + 1) & ZSMS_RX_RING_MASK;
409
410 /* low byte of ring_data is rr1 */
411 c = (ring_data >> 8) & 0xff;
412
413 if (ring_data & ZSRR1_DO)
414 intr_flags |= INTR_RX_OVERRUN;
415 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
416 log(LOG_ERR, "%s: input error (0x%x)\n",
417 zsms->zsms_dev.dv_xname, ring_data);
418 c = -1; /* signal input error */
419 }
420
421 /* Pass this up to the "middle" layer. */
422 zsms_input(zsms, c);
423 }
424 if (intr_flags & INTR_RX_OVERRUN) {
425 log(LOG_ERR, "%s: input overrun\n",
426 zsms->zsms_dev.dv_xname);
427 }
428 zsms->zsms_rbget = get;
429
430 if (intr_flags & INTR_TX_EMPTY) {
431 /*
432 * Transmit done. (Not expected.)
433 */
434 log(LOG_ERR, "%s: transmit interrupt?\n",
435 zsms->zsms_dev.dv_xname);
436 }
437
438 if (intr_flags & INTR_ST_CHECK) {
439 /*
440 * Status line change. (Not expected.)
441 */
442 log(LOG_ERR, "%s: status interrupt?\n",
443 zsms->zsms_dev.dv_xname);
444 cs->cs_rr0_delta = 0;
445 }
446
447 splx(s);
448 }
449
450 struct zsops zsops_zsms = {
451 zsms_rxint, /* receive char available */
452 zsms_stint, /* external/status */
453 zsms_txint, /* xmit buffer empty */
454 zsms_softint, /* process software interrupt */
455 };
456