xref: /netbsd-src/sys/dev/ofw/ofcons.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: ofcons.c,v 1.15 2001/11/13 07:26:28 lukem Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ofcons.c,v 1.15 2001/11/13 07:26:28 lukem Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40 #include <sys/proc.h>
41 #include <sys/systm.h>
42 #include <sys/callout.h>
43 #include <sys/tty.h>
44 
45 #include <dev/cons.h>
46 
47 #include <dev/ofw/openfirm.h>
48 
49 struct ofcons_softc {
50 	struct device of_dev;
51 	struct tty *of_tty;
52 	struct callout sc_poll_ch;
53 	int of_flags;
54 };
55 /* flags: */
56 #define	OFPOLL		1
57 
58 #define	OFBURSTLEN	128	/* max number of bytes to write in one chunk */
59 
60 cdev_decl(ofcons_);
61 cons_decl(ofcons_);
62 
63 static int stdin, stdout;
64 
65 static int ofcons_match __P((struct device *, struct cfdata *, void *));
66 static void ofcons_attach __P((struct device *, struct device *, void *));
67 
68 struct cfattach ofcons_ca = {
69 	sizeof(struct ofcons_softc), ofcons_match, ofcons_attach
70 };
71 
72 extern struct cfdriver ofcons_cd;
73 
74 static int ofcons_probe __P((void));
75 
76 static int
77 ofcons_match(parent, match, aux)
78 	struct device *parent;
79 	struct cfdata *match;
80 	void *aux;
81 {
82 	struct ofbus_attach_args *oba = aux;
83 
84 	if (strcmp(oba->oba_busname, "ofw"))
85 		return (0);
86 	if (!ofcons_probe())
87 		return 0;
88 	return OF_instance_to_package(stdin) == oba->oba_phandle
89 		|| OF_instance_to_package(stdout) == oba->oba_phandle;
90 }
91 
92 static void
93 ofcons_attach(parent, self, aux)
94 	struct device *parent, *self;
95 	void *aux;
96 {
97 	struct ofcons_softc *sc = (struct ofcons_softc *) self;
98 
99 	printf("\n");
100 
101 	callout_init(&sc->sc_poll_ch);
102 }
103 
104 static void ofcons_start __P((struct tty *));
105 static int ofcons_param __P((struct tty *, struct termios *));
106 static void ofcons_pollin __P((void *));
107 
108 int
109 ofcons_open(dev, flag, mode, p)
110 	dev_t dev;
111 	int flag, mode;
112 	struct proc *p;
113 {
114 	struct ofcons_softc *sc;
115 	int unit = minor(dev);
116 	struct tty *tp;
117 
118 	if (unit >= ofcons_cd.cd_ndevs)
119 		return ENXIO;
120 	sc = ofcons_cd.cd_devs[unit];
121 	if (!sc)
122 		return ENXIO;
123 	if (!(tp = sc->of_tty))
124 		sc->of_tty = tp = ttymalloc();
125 	tp->t_oproc = ofcons_start;
126 	tp->t_param = ofcons_param;
127 	tp->t_dev = dev;
128 	if (!(tp->t_state & TS_ISOPEN)) {
129 		ttychars(tp);
130 		tp->t_iflag = TTYDEF_IFLAG;
131 		tp->t_oflag = TTYDEF_OFLAG;
132 		tp->t_cflag = TTYDEF_CFLAG;
133 		tp->t_lflag = TTYDEF_LFLAG;
134 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
135 		ofcons_param(tp, &tp->t_termios);
136 		ttsetwater(tp);
137 	} else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
138 		return EBUSY;
139 	tp->t_state |= TS_CARR_ON;
140 
141 	if (!(sc->of_flags & OFPOLL)) {
142 		sc->of_flags |= OFPOLL;
143 		callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
144 	}
145 
146 	return (*tp->t_linesw->l_open)(dev, tp);
147 }
148 
149 int
150 ofcons_close(dev, flag, mode, p)
151 	dev_t dev;
152 	int flag, mode;
153 	struct proc *p;
154 {
155 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
156 	struct tty *tp = sc->of_tty;
157 
158 	callout_stop(&sc->sc_poll_ch);
159 	sc->of_flags &= ~OFPOLL;
160 	(*tp->t_linesw->l_close)(tp, flag);
161 	ttyclose(tp);
162 	return 0;
163 }
164 
165 int
166 ofcons_read(dev, uio, flag)
167 	dev_t dev;
168 	struct uio *uio;
169 	int flag;
170 {
171 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
172 	struct tty *tp = sc->of_tty;
173 
174 	return (*tp->t_linesw->l_read)(tp, uio, flag);
175 }
176 
177 int
178 ofcons_write(dev, uio, flag)
179 	dev_t dev;
180 	struct uio *uio;
181 	int flag;
182 {
183 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
184 	struct tty *tp = sc->of_tty;
185 
186 	return (*tp->t_linesw->l_write)(tp, uio, flag);
187 }
188 
189 int
190 ofcons_poll(dev, events, p)
191 	dev_t dev;
192 	int events;
193 	struct proc *p;
194 {
195 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
196 	struct tty *tp = sc->of_tty;
197 
198 	return ((*tp->t_linesw->l_poll)(tp, events, p));
199 }
200 int
201 ofcons_ioctl(dev, cmd, data, flag, p)
202 	dev_t dev;
203 	u_long cmd;
204 	caddr_t data;
205 	int flag;
206 	struct proc *p;
207 {
208 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
209 	struct tty *tp = sc->of_tty;
210 	int error;
211 
212 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) >= 0)
213 		return error;
214 	if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
215 		return error;
216 	return ENOTTY;
217 }
218 
219 struct tty *
220 ofcons_tty(dev)
221 	dev_t dev;
222 {
223 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
224 
225 	return sc->of_tty;
226 }
227 
228 void
229 ofcons_stop(tp, flag)
230 	struct tty *tp;
231 	int flag;
232 {
233 }
234 
235 static void
236 ofcons_start(tp)
237 	struct tty *tp;
238 {
239 	struct clist *cl;
240 	int s, len;
241 	u_char buf[OFBURSTLEN];
242 
243 	s = spltty();
244 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
245 		splx(s);
246 		return;
247 	}
248 	tp->t_state |= TS_BUSY;
249 	splx(s);
250 	cl = &tp->t_outq;
251 	len = q_to_b(cl, buf, OFBURSTLEN);
252 	OF_write(stdout, buf, len);
253 	s = spltty();
254 	tp->t_state &= ~TS_BUSY;
255 	if (cl->c_cc) {
256 		tp->t_state |= TS_TIMEOUT;
257 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
258 	}
259 	if (cl->c_cc <= tp->t_lowat) {
260 		if (tp->t_state & TS_ASLEEP) {
261 			tp->t_state &= ~TS_ASLEEP;
262 			wakeup(cl);
263 		}
264 		selwakeup(&tp->t_wsel);
265 	}
266 	splx(s);
267 }
268 
269 static int
270 ofcons_param(tp, t)
271 	struct tty *tp;
272 	struct termios *t;
273 {
274 	tp->t_ispeed = t->c_ispeed;
275 	tp->t_ospeed = t->c_ospeed;
276 	tp->t_cflag = t->c_cflag;
277 	return 0;
278 }
279 
280 static void
281 ofcons_pollin(aux)
282 	void *aux;
283 {
284 	struct ofcons_softc *sc = aux;
285 	struct tty *tp = sc->of_tty;
286 	char ch;
287 
288 	while (OF_read(stdin, &ch, 1) > 0) {
289 		if (tp && (tp->t_state & TS_ISOPEN))
290 			(*tp->t_linesw->l_rint)(ch, tp);
291 	}
292 	callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
293 }
294 
295 static int
296 ofcons_probe()
297 {
298 	int chosen;
299 	char stdinbuf[4], stdoutbuf[4];
300 
301 	if (stdin)
302 		return 1;
303 	if ((chosen = OF_finddevice("/chosen")) == -1)
304 		return 0;
305 	if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) !=
306 	      sizeof stdinbuf ||
307 	    OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) !=
308 	      sizeof stdoutbuf)
309 		return 0;
310 
311 	/* Decode properties. */
312 	stdin = of_decode_int(stdinbuf);
313 	stdout = of_decode_int(stdoutbuf);
314 
315 	return 1;
316 }
317 
318 void
319 ofcons_cnprobe(cd)
320 	struct consdev *cd;
321 {
322 	int maj;
323 
324 	if (!ofcons_probe())
325 		return;
326 
327 	for (maj = 0; maj < nchrdev; maj++)
328 		if (cdevsw[maj].d_open == ofcons_open)
329 			break;
330 	cd->cn_dev = makedev(maj, 0);
331 	cd->cn_pri = CN_INTERNAL;
332 }
333 
334 void
335 ofcons_cninit(cd)
336 	struct consdev *cd;
337 {
338 }
339 
340 int
341 ofcons_cngetc(dev)
342 	dev_t dev;
343 {
344 	unsigned char ch = '\0';
345 	int l;
346 
347 	while ((l = OF_read(stdin, &ch, 1)) != 1)
348 		if (l != -2 && l != 0)
349 			return -1;
350 	return ch;
351 }
352 
353 void
354 ofcons_cnputc(dev, c)
355 	dev_t dev;
356 	int c;
357 {
358 	char ch = c;
359 
360 	OF_write(stdout, &ch, 1);
361 }
362 
363 void
364 ofcons_cnpollc(dev, on)
365 	dev_t dev;
366 	int on;
367 {
368 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
369 
370 	if (!sc)
371 		return;
372 	if (on) {
373 		if (sc->of_flags & OFPOLL)
374 			callout_stop(&sc->sc_poll_ch);
375 		sc->of_flags &= ~OFPOLL;
376 	} else {
377 		if (!(sc->of_flags & OFPOLL)) {
378 			sc->of_flags |= OFPOLL;
379 			callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
380 		}
381 	}
382 }
383