xref: /netbsd-src/sys/dev/sun/sunms.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: sunms.c,v 1.27 2007/07/09 21:01:23 ad 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.27 2007/07/09 21:01:23 ad 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(struct device *, struct cfdata *, void *);
92 static void	sunms_attach(struct device *, struct device *, void *);
93 static int	sunmsiopen(struct device *, int mode);
94 int	sunmsinput(int, struct tty *);
95 
96 CFATTACH_DECL(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
126 sunms_match(parent, cf, aux)
127 	struct device *parent;
128 	struct cfdata *cf;
129 	void   *aux;
130 {
131 	struct kbd_ms_tty_attach_args *args = aux;
132 
133 	if (sunms_bps == 0)
134 		return 0;
135 
136 	if (strcmp(args->kmta_name, "mouse") == 0)
137 		return (1);
138 
139 	return 0;
140 }
141 
142 void
143 sunms_attach(parent, self, aux)
144 	struct device *parent, *self;
145 	void   *aux;
146 
147 {
148 	struct ms_softc *ms = device_private(self);
149 	struct kbd_ms_tty_attach_args *args = aux;
150 	struct cfdata *cf;
151 	struct tty *tp = args->kmta_tp;
152 	int ms_unit;
153 #if NWSMOUSE > 0
154 	struct wsmousedev_attach_args a;
155 #endif
156 
157 	cf = device_cfdata(&ms->ms_dev);
158 	ms_unit = device_unit(&ms->ms_dev);
159 	tp->t_sc  = ms;
160 	tp->t_dev = args->kmta_dev;
161 	ms->ms_cs = (struct zs_chanstate *)tp;
162         ms->ms_deviopen = sunmsiopen;
163         ms->ms_deviclose = NULL;
164 
165 	printf("\n");
166 
167 	/* Initialize the speed, etc. */
168 	if (ttyldisc_attach(&sunms_disc) != 0)
169 		panic("sunms_attach: sunms_disc");
170 	ttyldisc_release(tp->t_linesw);
171 	tp->t_linesw = ttyldisc_lookup(sunms_disc.l_name);
172 	KASSERT(tp->t_linesw == &sunms_disc);
173 	tp->t_oflag &= ~OPOST;
174 
175 	/* Initialize translator. */
176 	ms->ms_byteno = -1;
177 
178 #if NWSMOUSE > 0
179 	/*
180 	 * attach wsmouse
181 	 */
182 	a.accessops = &sunms_accessops;
183 	a.accesscookie = ms;
184 
185 	ms->ms_wsmousedev = config_found(self, &a, wsmousedevprint);
186 #endif
187 }
188 
189 /*
190  * Internal open routine.  This really should be inside com.c
191  * But I'm putting it here until we have a generic internal open
192  * mechanism.
193  */
194 int
195 sunmsiopen(dev, flags)
196 	struct device *dev;
197 	int flags;
198 {
199 	struct ms_softc *ms = (void *) dev;
200 	struct tty *tp = (struct tty *)ms->ms_cs;
201 	struct lwp *l = curlwp ? curlwp : &lwp0;
202 	struct termios t;
203 	int error;
204 
205 	/* Open the lower device */
206 	if ((error = cdev_open(tp->t_dev, O_NONBLOCK|flags,
207 				     0/* ignored? */, l)) != 0)
208 		return (error);
209 
210 	/* Now configure it for the console. */
211 	tp->t_ospeed = 0;
212 	t.c_ispeed = sunms_bps;
213 	t.c_ospeed = sunms_bps;
214 	t.c_cflag =  CLOCAL|CS8;
215 	(*tp->t_param)(tp, &t);
216 
217 	return (0);
218 }
219 
220 int
221 sunmsinput(c, tp)
222 	int c;
223 	struct tty *tp;
224 {
225 	struct ms_softc *ms = (struct ms_softc *)tp->t_sc;
226 
227 	if (c & TTY_ERRORMASK) c = -1;
228 	else c &= TTY_CHARMASK;
229 
230 	/* Pass this up to the "middle" layer. */
231 	ms_input(ms, c);
232 	return (0);
233 }
234 
235 int
236 sunms_ioctl(v, cmd, data, flag, l)
237 	void *v;
238 	u_long cmd;
239 	void *data;
240 	int flag;
241 	struct lwp *l;
242 {
243 /*	struct ms_softc *sc = v; */
244 
245 	switch (cmd) {
246 	case WSMOUSEIO_GTYPE:
247 		*(u_int *)data = WSMOUSE_TYPE_PS2; /* XXX  */
248 		break;
249 
250 	default:
251 		return (EPASSTHROUGH);
252 	}
253 	return (0);
254 }
255 
256 int
257 sunms_enable(v)
258 	void *v;
259 {
260 	struct ms_softc *ms = v;
261 	int err;
262 	int s;
263 
264 	if (ms->ms_ready)
265 		return EBUSY;
266 
267 	err = sunmsiopen(v, 0);
268 	if (err)
269 		return err;
270 
271 	s = spltty();
272 	ms->ms_ready = 2;
273 	splx(s);
274 
275 	return 0;
276 }
277 
278 void
279 sunms_disable(v)
280 	void *v;
281 {
282 	struct ms_softc *ms = v;
283 	int s;
284 
285 	s = spltty();
286 	ms->ms_ready = 0;
287 	splx(s);
288 }
289 #endif
290