xref: /netbsd-src/sys/dev/sun/sunms.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: sunms.c,v 1.35 2021/08/07 16:19:16 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.35 2021/08/07 16:19:16 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 <dev/sun/event_var.h>
75 #include <dev/sun/msvar.h>
76 #include <dev/sun/kbd_ms_ttyvar.h>
77 
78 #include <dev/wscons/wsconsio.h>
79 #include <dev/wscons/wsmousevar.h>
80 
81 #include "ms.h"
82 #include "wsmouse.h"
83 #if NMS > 0
84 
85 #ifdef SUN_MS_BPS
86 int	sunms_bps = SUN_MS_BPS;
87 #else
88 int	sunms_bps = MS_DEFAULT_BPS;
89 #endif
90 
91 static int	sunms_match(device_t, cfdata_t, void *);
92 static void	sunms_attach(device_t, device_t, void *);
93 static int	sunmsiopen(device_t, int mode);
94 int	sunmsinput(int, struct tty *);
95 
96 CFATTACH_DECL_NEW(ms_tty, sizeof(struct ms_softc),
97     sunms_match, sunms_attach, NULL, NULL);
98 
99 struct linesw sunms_disc = {
100 	.l_name = "sunms",
101 	.l_open = ttylopen,
102 	.l_close = ttylclose,
103 	.l_read = ttyerrio,
104 	.l_write = ttyerrio,
105 	.l_ioctl = ttynullioctl,
106 	.l_rint = sunmsinput,
107 	.l_start = ttstart,
108 	.l_modem = nullmodem,
109 	.l_poll = ttpoll
110 };
111 
112 int	sunms_enable(void *);
113 int	sunms_ioctl(void *, u_long, void *, int, struct lwp *);
114 void	sunms_disable(void *);
115 
116 const struct wsmouse_accessops	sunms_accessops = {
117 	sunms_enable,
118 	sunms_ioctl,
119 	sunms_disable,
120 };
121 
122 /*
123  * ms_match: how is this zs channel configured?
124  */
125 int
sunms_match(device_t parent,cfdata_t cf,void * aux)126 sunms_match(device_t parent, cfdata_t cf, void *aux)
127 {
128 	struct kbd_ms_tty_attach_args *args = aux;
129 
130 	if (sunms_bps == 0)
131 		return 0;
132 
133 	if (strcmp(args->kmta_name, "mouse") == 0)
134 		return (1);
135 
136 	return 0;
137 }
138 
139 void
sunms_attach(device_t parent,device_t self,void * aux)140 sunms_attach(device_t parent, device_t self, void *aux)
141 {
142 	struct ms_softc *ms = device_private(self);
143 	struct kbd_ms_tty_attach_args *args = aux;
144 	struct tty *tp = args->kmta_tp;
145 #if NWSMOUSE > 0
146 	struct wsmousedev_attach_args a;
147 #endif
148 
149 	ms->ms_dev = self;
150 	tp->t_sc  = ms;
151 	tp->t_dev = args->kmta_dev;
152 	ms->ms_priv = tp;
153 	ms->ms_deviopen = sunmsiopen;
154 	ms->ms_deviclose = NULL;
155 
156 	aprint_normal("\n");
157 
158 	/* Initialize the speed, etc. */
159 	if (ttyldisc_attach(&sunms_disc) != 0)
160 		panic("sunms_attach: sunms_disc");
161 	ttyldisc_release(tp->t_linesw);
162 	tp->t_linesw = ttyldisc_lookup(sunms_disc.l_name);
163 	KASSERT(tp->t_linesw == &sunms_disc);
164 	tp->t_oflag &= ~OPOST;
165 	SET(tp->t_state, TS_KERN_ONLY);
166 
167 	/* Initialize translator. */
168 	ms->ms_byteno = -1;
169 
170 #if NWSMOUSE > 0
171 	/*
172 	 * attach wsmouse
173 	 */
174 	a.accessops = &sunms_accessops;
175 	a.accesscookie = ms;
176 
177 	ms->ms_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE);
178 #endif
179 }
180 
181 /*
182  * Internal open routine.  This really should be inside com.c
183  * But I'm putting it here until we have a generic internal open
184  * mechanism.
185  */
186 int
sunmsiopen(device_t dev,int flags)187 sunmsiopen(device_t dev, int flags)
188 {
189 	struct ms_softc *ms = device_private(dev);
190 	struct tty *tp = ms->ms_priv;
191 	struct lwp *l = curlwp ? curlwp : &lwp0;
192 	struct termios t;
193 	int error;
194 
195 	/* Open the lower device */
196 	CLR(tp->t_state, TS_KERN_ONLY);
197 	if ((error = cdev_open(tp->t_dev, O_NONBLOCK|flags,
198 				     0/* ignored? */, l)) != 0)
199 		return (error);
200 
201 	/* Now configure it for the console. */
202 	tp->t_ospeed = 0;
203 	t.c_ispeed = sunms_bps;
204 	t.c_ospeed = sunms_bps;
205 	t.c_cflag =  CLOCAL|CS8;
206 	(*tp->t_param)(tp, &t);
207 	SET(tp->t_state, TS_KERN_ONLY);
208 
209 	return (0);
210 }
211 
212 int
sunmsinput(int c,struct tty * tp)213 sunmsinput(int c, struct tty *tp)
214 {
215 	struct ms_softc *ms = tp->t_sc;
216 
217 	if (c & TTY_ERRORMASK) c = -1;
218 	else c &= TTY_CHARMASK;
219 
220 	/* Pass this up to the "middle" layer. */
221 	ms_input(ms, c);
222 	return (0);
223 }
224 
225 int
sunms_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)226 sunms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
227 {
228 /*	struct ms_softc *sc = v; */
229 
230 	switch (cmd) {
231 	case WSMOUSEIO_GTYPE:
232 		*(u_int *)data = WSMOUSE_TYPE_PS2; /* XXX  */
233 		break;
234 
235 	default:
236 		return (EPASSTHROUGH);
237 	}
238 	return (0);
239 }
240 
241 int
sunms_enable(void * v)242 sunms_enable(void *v)
243 {
244 	struct ms_softc *ms = v;
245 	int err;
246 	int s;
247 
248 	if (ms->ms_ready)
249 		return EBUSY;
250 
251 	err = sunmsiopen(ms->ms_dev, 0);
252 	if (err)
253 		return err;
254 
255 	s = spltty();
256 	ms->ms_ready = 2;
257 	splx(s);
258 
259 	return 0;
260 }
261 
262 void
sunms_disable(void * v)263 sunms_disable(void *v)
264 {
265 	struct ms_softc *ms = v;
266 	int s;
267 
268 	s = spltty();
269 	ms->ms_ready = 0;
270 	splx(s);
271 }
272 #endif
273