dzms.c revision 1.22 1 /* $NetBSD: dzms.c,v 1.22 2012/10/27 17:18:15 chs 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 to line 1 of the DZ*
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: dzms.c,v 1.22 2012/10/27 17:18:15 chs 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 <sys/bus.h>
60
61 #include <dev/dec/dzreg.h>
62 #include <dev/dec/dzvar.h>
63 #include <dev/dec/dzkbdvar.h>
64 #include <dev/dec/lk201.h>
65
66 #include <dev/wscons/wsconsio.h>
67 #include <dev/wscons/wsmousevar.h>
68
69 #include "locators.h"
70
71 struct dzms_softc { /* driver status information */
72 struct dz_linestate *dzms_ls;
73
74 int sc_enabled; /* input enabled? */
75 int sc_selftest;
76
77 int inputstate;
78 u_int buttons;
79 int dx, dy;
80
81 device_t sc_wsmousedev;
82 };
83
84 static int dzms_match(device_t, cfdata_t, void *);
85 static void dzms_attach(device_t, device_t, void *);
86 static int dzms_input(void *, int);
87
88 CFATTACH_DECL_NEW(dzms, sizeof(struct dzms_softc),
89 dzms_match, dzms_attach, NULL, NULL);
90
91 static int dzms_enable(void *);
92 static int dzms_ioctl(void *, u_long, void *, int, struct lwp *);
93 static void dzms_disable(void *);
94
95 const struct wsmouse_accessops dzms_accessops = {
96 dzms_enable,
97 dzms_ioctl,
98 dzms_disable,
99 };
100
101 static int
102 dzms_match(device_t parent, cfdata_t cf, void *aux)
103 {
104 struct dzkm_attach_args *daa = aux;
105
106 /* Exact match is better than wildcard. */
107 if (cf->cf_loc[DZCF_LINE] == daa->daa_line)
108 return 2;
109
110 /* This driver accepts wildcard. */
111 if (cf->cf_loc[DZCF_LINE] == DZCF_LINE_DEFAULT)
112 return 1;
113
114 return 0;
115 }
116
117 static void
118 dzms_attach(device_t parent, device_t self, void *aux)
119 {
120 struct dz_softc *dz = device_private(parent);
121 struct dzms_softc *dzms = device_private(self);
122 struct dzkm_attach_args *daa = aux;
123 struct dz_linestate *ls;
124 struct wsmousedev_attach_args a;
125
126 dz->sc_dz[daa->daa_line].dz_catch = dzms_input;
127 dz->sc_dz[daa->daa_line].dz_private = dzms;
128 ls = &dz->sc_dz[daa->daa_line];
129 dzms->dzms_ls = ls;
130
131 printf("\n");
132
133 a.accessops = &dzms_accessops;
134 a.accesscookie = dzms;
135
136 dzms->sc_enabled = 0;
137 dzms->sc_selftest = 0;
138 dzms->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
139 }
140
141 static int
142 dzms_enable(void *v)
143 {
144 struct dzms_softc *sc = v;
145
146 if (sc->sc_enabled)
147 return EBUSY;
148
149 sc->sc_selftest = 4; /* wait for 4 byte reply upto 1/2 sec */
150 dzputc(sc->dzms_ls, MOUSE_SELF_TEST);
151 (void)tsleep(dzms_enable, TTIPRI, "dzmsopen", hz / 2);
152 if (sc->sc_selftest != 0) {
153 sc->sc_selftest = 0;
154 return ENXIO;
155 }
156 DELAY(150);
157 dzputc(sc->dzms_ls, MOUSE_INCREMENTAL);
158 sc->sc_enabled = 1;
159 sc->inputstate = 0;
160 return 0;
161 }
162
163 static void
164 dzms_disable(void *v)
165 {
166 struct dzms_softc *sc = v;
167
168 sc->sc_enabled = 0;
169 }
170
171 static int
172 dzms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
173 {
174 if (cmd == WSMOUSEIO_GTYPE) {
175 *(u_int *)data = WSMOUSE_TYPE_VSXXX;
176 return 0;
177 }
178 return EPASSTHROUGH;
179 }
180
181 static int
182 dzms_input(void *vsc, int data)
183 {
184 struct dzms_softc *sc = vsc;
185
186 if (sc->sc_enabled == 0) {
187 if (sc->sc_selftest > 0) {
188 sc->sc_selftest -= 1;
189 if (sc->sc_selftest == 0)
190 wakeup(dzms_enable);
191 }
192 return (1);
193 }
194
195 #define WSMS_BUTTON1 0x01
196 #define WSMS_BUTTON2 0x02
197 #define WSMS_BUTTON3 0x04
198
199 if ((data & MOUSE_START_FRAME) != 0)
200 sc->inputstate = 1;
201 else
202 sc->inputstate++;
203
204 if (sc->inputstate == 1) {
205 sc->buttons = 0;
206 if ((data & LEFT_BUTTON) != 0)
207 sc->buttons |= WSMS_BUTTON1;
208 if ((data & MIDDLE_BUTTON) != 0)
209 sc->buttons |= WSMS_BUTTON2;
210 if ((data & RIGHT_BUTTON) != 0)
211 sc->buttons |= WSMS_BUTTON3;
212
213 sc->dx = data & MOUSE_X_SIGN;
214 sc->dy = data & MOUSE_Y_SIGN;
215 } else if (sc->inputstate == 2) {
216 if (sc->dx == 0)
217 sc->dx = -data;
218 else
219 sc->dx = data;
220 } else if (sc->inputstate == 3) {
221 sc->inputstate = 0;
222 if (sc->dy == 0)
223 sc->dy = -data;
224 else
225 sc->dy = data;
226 wsmouse_input(sc->sc_wsmousedev,
227 sc->buttons,
228 sc->dx, sc->dy, 0, 0,
229 WSMOUSE_INPUT_DELTA);
230 }
231
232 return(1);
233 }
234
235