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