xref: /netbsd-src/sys/dev/sun/sunms.c (revision 8e6ab8837d8d6b9198e67c1c445300b483e2f304)
1 /*	$NetBSD: sunms.c,v 1.16 2003/06/29 22:30:49 fvdl 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. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
45  */
46 
47 /*
48  * Mouse driver (/dev/mouse)
49  */
50 
51 /*
52  * Zilog Z8530 Dual UART driver (mouse interface)
53  *
54  * This is the "slave" driver that will be attached to
55  * the "zsc" driver for a Sun mouse.
56  */
57 
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: sunms.c,v 1.16 2003/06/29 22:30:49 fvdl Exp $");
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/conf.h>
64 #include <sys/device.h>
65 #include <sys/ioctl.h>
66 #include <sys/kernel.h>
67 #include <sys/proc.h>
68 #include <sys/signal.h>
69 #include <sys/signalvar.h>
70 #include <sys/time.h>
71 #include <sys/select.h>
72 #include <sys/syslog.h>
73 #include <sys/fcntl.h>
74 #include <sys/tty.h>
75 
76 #include <machine/vuid_event.h>
77 
78 #include <sys/tty.h>
79 #include <dev/sun/event_var.h>
80 #include <dev/sun/msvar.h>
81 #include <dev/sun/kbd_ms_ttyvar.h>
82 
83 #include <dev/wscons/wsconsio.h>
84 #include <dev/wscons/wsmousevar.h>
85 
86 #include "ms.h"
87 #include "wsmouse.h"
88 #if NMS > 0
89 
90 #ifdef SUN_MS_BPS
91 int	sunms_bps = SUN_MS_BPS;
92 #else
93 int	sunms_bps = MS_DEFAULT_BPS;
94 #endif
95 
96 static int	sunms_match(struct device *, struct cfdata *, void *);
97 static void	sunms_attach(struct device *, struct device *, void *);
98 static int	sunmsiopen(struct device *, int mode);
99 int	sunmsinput(int, struct tty *);
100 
101 CFATTACH_DECL(ms_tty, sizeof(struct ms_softc),
102     sunms_match, sunms_attach, NULL, NULL);
103 
104 struct  linesw sunms_disc =
105 	{ "sunms", 8, ttylopen, ttylclose, ttyerrio, ttyerrio, ttynullioctl,
106 	  sunmsinput, ttstart, nullmodem, ttpoll };	/* 8- SUNMOUSEDISC */
107 
108 int	sunms_enable(void *);
109 int	sunms_ioctl(void *, u_long, caddr_t, int, struct proc *);
110 void	sunms_disable(void *);
111 
112 const struct wsmouse_accessops	sunms_accessops = {
113 	sunms_enable,
114 	sunms_ioctl,
115 	sunms_disable,
116 };
117 
118 /*
119  * ms_match: how is this zs channel configured?
120  */
121 int
122 sunms_match(parent, cf, aux)
123 	struct device *parent;
124 	struct cfdata *cf;
125 	void   *aux;
126 {
127 	struct kbd_ms_tty_attach_args *args = aux;
128 
129 	if (sunms_bps == 0)
130 		return 0;
131 
132 	if (strcmp(args->kmta_name, "mouse") == 0)
133 		return (1);
134 
135 	return 0;
136 }
137 
138 void
139 sunms_attach(parent, self, aux)
140 	struct device *parent, *self;
141 	void   *aux;
142 
143 {
144 	struct ms_softc *ms = (void *) self;
145 	struct kbd_ms_tty_attach_args *args = aux;
146 	struct cfdata *cf;
147 	struct tty *tp = args->kmta_tp;
148 	int ms_unit;
149 #if NWSMOUSE > 0
150 	struct wsmousedev_attach_args a;
151 #endif
152 
153 	cf = ms->ms_dev.dv_cfdata;
154 	ms_unit = ms->ms_dev.dv_unit;
155 	tp->t_sc  = ms;
156 	tp->t_dev = args->kmta_dev;
157 	ms->ms_cs = (struct zs_chanstate *)tp;
158         ms->ms_deviopen = sunmsiopen;
159         ms->ms_deviclose = NULL;
160 
161 	printf("\n");
162 
163 	/* Initialize the speed, etc. */
164 	if (ttyldisc_add(&sunms_disc, -1) == -1)
165 		panic("sunms_attach: sunms_disc");
166 	tp->t_linesw = &sunms_disc;
167 	tp->t_oflag &= ~OPOST;
168 
169 	/* Initialize translator. */
170 	ms->ms_byteno = -1;
171 
172 #if NWSMOUSE > 0
173 	/*
174 	 * attach wsmouse
175 	 */
176 	a.accessops = &sunms_accessops;
177 	a.accesscookie = ms;
178 
179 	ms->ms_wsmousedev = config_found(self, &a, wsmousedevprint);
180 #endif
181 }
182 
183 /*
184  * Internal open routine.  This really should be inside com.c
185  * But I'm putting it here until we have a generic internal open
186  * mechanism.
187  */
188 int
189 sunmsiopen(dev, flags)
190 	struct device *dev;
191 	int flags;
192 {
193 	struct ms_softc *ms = (void *) dev;
194 	struct tty *tp = (struct tty *)ms->ms_cs;
195 	struct proc *p = curproc ? curproc : &proc0;
196 	struct termios t;
197 	const struct cdevsw *cdev;
198 	int error;
199 
200 	cdev = cdevsw_lookup(tp->t_dev);
201 	if (cdev == NULL)
202 		return (ENXIO);
203 
204 	/* Open the lower device */
205 	if ((error = (*cdev->d_open)(tp->t_dev, O_NONBLOCK|flags,
206 				     0/* ignored? */, p)) != 0)
207 		return (error);
208 
209 	/* Now configure it for the console. */
210 	tp->t_ospeed = 0;
211 	t.c_ispeed = sunms_bps;
212 	t.c_ospeed = sunms_bps;
213 	t.c_cflag =  CLOCAL|CS8;
214 	(*tp->t_param)(tp, &t);
215 
216 	return (0);
217 }
218 
219 int
220 sunmsinput(c, tp)
221 	int c;
222 	struct tty *tp;
223 {
224 	struct ms_softc *ms = (struct ms_softc *)tp->t_sc;
225 
226 	if (c & TTY_ERRORMASK) c = -1;
227 	else c &= TTY_CHARMASK;
228 
229 	/* Pass this up to the "middle" layer. */
230 	ms_input(ms, c);
231 	return (0);
232 }
233 
234 int
235 sunms_ioctl(v, cmd, data, flag, p)
236 	void *v;
237 	u_long cmd;
238 	caddr_t data;
239 	int flag;
240 	struct proc *p;
241 {
242 /*	struct ms_softc *sc = v; */
243 
244 	switch (cmd) {
245 	case WSMOUSEIO_GTYPE:
246 		*(u_int *)data = WSMOUSE_TYPE_PS2; /* XXX  */
247 		break;
248 
249 	default:
250 		return (EPASSTHROUGH);
251 	}
252 	return (0);
253 }
254 
255 int
256 sunms_enable(v)
257 	void *v;
258 {
259 	struct ms_softc *ms = v;
260 	int err;
261 	int s;
262 
263 	if (ms->ms_ready)
264 		return EBUSY;
265 
266 	err = sunmsiopen(v, 0);
267 	if (err)
268 		return err;
269 
270 	s = spltty();
271 	ms->ms_ready = 2;
272 	splx(s);
273 
274 	return 0;
275 }
276 
277 void
278 sunms_disable(v)
279 	void *v;
280 {
281 	struct ms_softc *ms = v;
282 	int s;
283 
284 	s = spltty();
285 	ms->ms_ready = 0;
286 	splx(s);
287 }
288 #endif
289