xref: /netbsd-src/sys/dev/sun/sunms.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: sunms.c,v 1.24 2006/03/30 16:12:10 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  * Mouse driver (/dev/mouse)
45  */
46 
47 /*
48  * Zilog Z8530 Dual UART driver (mouse interface)
49  *
50  * This is the "slave" driver that will be attached to
51  * the "zsc" driver for a Sun mouse.
52  */
53 
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: sunms.c,v 1.24 2006/03/30 16:12:10 thorpej Exp $");
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/conf.h>
60 #include <sys/device.h>
61 #include <sys/ioctl.h>
62 #include <sys/kernel.h>
63 #include <sys/proc.h>
64 #include <sys/signal.h>
65 #include <sys/signalvar.h>
66 #include <sys/time.h>
67 #include <sys/select.h>
68 #include <sys/syslog.h>
69 #include <sys/fcntl.h>
70 #include <sys/tty.h>
71 
72 #include <machine/vuid_event.h>
73 
74 #include <sys/tty.h>
75 #include <dev/sun/event_var.h>
76 #include <dev/sun/msvar.h>
77 #include <dev/sun/kbd_ms_ttyvar.h>
78 
79 #include <dev/wscons/wsconsio.h>
80 #include <dev/wscons/wsmousevar.h>
81 
82 #include "ms.h"
83 #include "wsmouse.h"
84 #if NMS > 0
85 
86 #ifdef SUN_MS_BPS
87 int	sunms_bps = SUN_MS_BPS;
88 #else
89 int	sunms_bps = MS_DEFAULT_BPS;
90 #endif
91 
92 static int	sunms_match(struct device *, struct cfdata *, void *);
93 static void	sunms_attach(struct device *, struct device *, void *);
94 static int	sunmsiopen(struct device *, int mode);
95 int	sunmsinput(int, struct tty *);
96 
97 CFATTACH_DECL(ms_tty, sizeof(struct ms_softc),
98     sunms_match, sunms_attach, NULL, NULL);
99 
100 struct linesw sunms_disc = {
101 	.l_name = "sunms",
102 	.l_open = ttylopen,
103 	.l_close = ttylclose,
104 	.l_read = ttyerrio,
105 	.l_write = ttyerrio,
106 	.l_ioctl = ttynullioctl,
107 	.l_rint = sunmsinput,
108 	.l_start = ttstart,
109 	.l_modem = nullmodem,
110 	.l_poll = ttpoll
111 };
112 
113 int	sunms_enable(void *);
114 int	sunms_ioctl(void *, u_long, caddr_t, int, struct lwp *);
115 void	sunms_disable(void *);
116 
117 const struct wsmouse_accessops	sunms_accessops = {
118 	sunms_enable,
119 	sunms_ioctl,
120 	sunms_disable,
121 };
122 
123 /*
124  * ms_match: how is this zs channel configured?
125  */
126 int
127 sunms_match(parent, cf, aux)
128 	struct device *parent;
129 	struct cfdata *cf;
130 	void   *aux;
131 {
132 	struct kbd_ms_tty_attach_args *args = aux;
133 
134 	if (sunms_bps == 0)
135 		return 0;
136 
137 	if (strcmp(args->kmta_name, "mouse") == 0)
138 		return (1);
139 
140 	return 0;
141 }
142 
143 void
144 sunms_attach(parent, self, aux)
145 	struct device *parent, *self;
146 	void   *aux;
147 
148 {
149 	struct ms_softc *ms = device_private(self);
150 	struct kbd_ms_tty_attach_args *args = aux;
151 	struct cfdata *cf;
152 	struct tty *tp = args->kmta_tp;
153 	int ms_unit;
154 #if NWSMOUSE > 0
155 	struct wsmousedev_attach_args a;
156 #endif
157 
158 	cf = device_cfdata(&ms->ms_dev);
159 	ms_unit = device_unit(&ms->ms_dev);
160 	tp->t_sc  = ms;
161 	tp->t_dev = args->kmta_dev;
162 	ms->ms_cs = (struct zs_chanstate *)tp;
163         ms->ms_deviopen = sunmsiopen;
164         ms->ms_deviclose = NULL;
165 
166 	printf("\n");
167 
168 	/* Initialize the speed, etc. */
169 	if (ttyldisc_attach(&sunms_disc) != 0)
170 		panic("sunms_attach: sunms_disc");
171 	ttyldisc_release(tp->t_linesw);
172 	tp->t_linesw = ttyldisc_lookup(sunms_disc.l_name);
173 	KASSERT(tp->t_linesw == &sunms_disc);
174 	tp->t_oflag &= ~OPOST;
175 
176 	/* Initialize translator. */
177 	ms->ms_byteno = -1;
178 
179 #if NWSMOUSE > 0
180 	/*
181 	 * attach wsmouse
182 	 */
183 	a.accessops = &sunms_accessops;
184 	a.accesscookie = ms;
185 
186 	ms->ms_wsmousedev = config_found(self, &a, wsmousedevprint);
187 #endif
188 }
189 
190 /*
191  * Internal open routine.  This really should be inside com.c
192  * But I'm putting it here until we have a generic internal open
193  * mechanism.
194  */
195 int
196 sunmsiopen(dev, flags)
197 	struct device *dev;
198 	int flags;
199 {
200 	struct ms_softc *ms = (void *) dev;
201 	struct tty *tp = (struct tty *)ms->ms_cs;
202 	struct lwp *l = curlwp ? curlwp : &lwp0;
203 	struct termios t;
204 	const struct cdevsw *cdev;
205 	int error;
206 
207 	cdev = cdevsw_lookup(tp->t_dev);
208 	if (cdev == NULL)
209 		return (ENXIO);
210 
211 	/* Open the lower device */
212 	if ((error = (*cdev->d_open)(tp->t_dev, O_NONBLOCK|flags,
213 				     0/* ignored? */, l)) != 0)
214 		return (error);
215 
216 	/* Now configure it for the console. */
217 	tp->t_ospeed = 0;
218 	t.c_ispeed = sunms_bps;
219 	t.c_ospeed = sunms_bps;
220 	t.c_cflag =  CLOCAL|CS8;
221 	(*tp->t_param)(tp, &t);
222 
223 	return (0);
224 }
225 
226 int
227 sunmsinput(c, tp)
228 	int c;
229 	struct tty *tp;
230 {
231 	struct ms_softc *ms = (struct ms_softc *)tp->t_sc;
232 
233 	if (c & TTY_ERRORMASK) c = -1;
234 	else c &= TTY_CHARMASK;
235 
236 	/* Pass this up to the "middle" layer. */
237 	ms_input(ms, c);
238 	return (0);
239 }
240 
241 int
242 sunms_ioctl(v, cmd, data, flag, l)
243 	void *v;
244 	u_long cmd;
245 	caddr_t data;
246 	int flag;
247 	struct lwp *l;
248 {
249 /*	struct ms_softc *sc = v; */
250 
251 	switch (cmd) {
252 	case WSMOUSEIO_GTYPE:
253 		*(u_int *)data = WSMOUSE_TYPE_PS2; /* XXX  */
254 		break;
255 
256 	default:
257 		return (EPASSTHROUGH);
258 	}
259 	return (0);
260 }
261 
262 int
263 sunms_enable(v)
264 	void *v;
265 {
266 	struct ms_softc *ms = v;
267 	int err;
268 	int s;
269 
270 	if (ms->ms_ready)
271 		return EBUSY;
272 
273 	err = sunmsiopen(v, 0);
274 	if (err)
275 		return err;
276 
277 	s = spltty();
278 	ms->ms_ready = 2;
279 	splx(s);
280 
281 	return 0;
282 }
283 
284 void
285 sunms_disable(v)
286 	void *v;
287 {
288 	struct ms_softc *ms = v;
289 	int s;
290 
291 	s = spltty();
292 	ms->ms_ready = 0;
293 	splx(s);
294 }
295 #endif
296