xref: /netbsd-src/sys/dev/ofw/ofcons.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: ofcons.c,v 1.24 2005/12/11 12:22:48 christos 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.24 2005/12/11 12:22:48 christos 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 cons_decl(ofcons_);
61 
62 static int stdin, stdout;
63 
64 static int ofcons_match(struct device *, struct cfdata *, void *);
65 static void ofcons_attach(struct device *, struct device *, void *);
66 
67 CFATTACH_DECL(ofcons, sizeof(struct ofcons_softc),
68     ofcons_match, ofcons_attach, NULL, NULL);
69 
70 extern struct cfdriver ofcons_cd;
71 
72 dev_type_open(ofcons_open);
73 dev_type_close(ofcons_close);
74 dev_type_read(ofcons_read);
75 dev_type_write(ofcons_write);
76 dev_type_ioctl(ofcons_ioctl);
77 dev_type_tty(ofcons_tty);
78 dev_type_poll(ofcons_poll);
79 
80 const struct cdevsw ofcons_cdevsw = {
81 	ofcons_open, ofcons_close, ofcons_read, ofcons_write, ofcons_ioctl,
82 	nostop, ofcons_tty, ofcons_poll, nommap, ttykqfilter, D_TTY
83 };
84 
85 static int ofcons_probe(void);
86 
87 static int
88 ofcons_match(parent, match, aux)
89 	struct device *parent;
90 	struct cfdata *match;
91 	void *aux;
92 {
93 	struct ofbus_attach_args *oba = aux;
94 
95 	if (strcmp(oba->oba_busname, "ofw"))
96 		return (0);
97 	if (!ofcons_probe())
98 		return 0;
99 	return OF_instance_to_package(stdin) == oba->oba_phandle
100 		|| OF_instance_to_package(stdout) == oba->oba_phandle;
101 }
102 
103 static void
104 ofcons_attach(parent, self, aux)
105 	struct device *parent, *self;
106 	void *aux;
107 {
108 	struct ofcons_softc *sc = (struct ofcons_softc *) self;
109 
110 	printf("\n");
111 
112 	callout_init(&sc->sc_poll_ch);
113 }
114 
115 static void ofcons_start(struct tty *);
116 static int ofcons_param(struct tty *, struct termios *);
117 static void ofcons_pollin(void *);
118 
119 int
120 ofcons_open(dev, flag, mode, l)
121 	dev_t dev;
122 	int flag, mode;
123 	struct lwp *l;
124 {
125 	struct ofcons_softc *sc;
126 	int unit = minor(dev);
127 	struct tty *tp;
128 
129 	if (unit >= ofcons_cd.cd_ndevs)
130 		return ENXIO;
131 	sc = ofcons_cd.cd_devs[unit];
132 	if (!sc)
133 		return ENXIO;
134 	if (!(tp = sc->of_tty))
135 		sc->of_tty = tp = ttymalloc();
136 	tp->t_oproc = ofcons_start;
137 	tp->t_param = ofcons_param;
138 	tp->t_dev = dev;
139 	if (!(tp->t_state & TS_ISOPEN)) {
140 		ttychars(tp);
141 		tp->t_iflag = TTYDEF_IFLAG;
142 		tp->t_oflag = TTYDEF_OFLAG;
143 		tp->t_cflag = TTYDEF_CFLAG;
144 		tp->t_lflag = TTYDEF_LFLAG;
145 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
146 		ofcons_param(tp, &tp->t_termios);
147 		ttsetwater(tp);
148 	} else if ((tp->t_state&TS_XCLUDE) &&
149 	    suser(l->l_proc->p_ucred, &l->l_proc->p_acflag))
150 		return EBUSY;
151 	tp->t_state |= TS_CARR_ON;
152 
153 	if (!(sc->of_flags & OFPOLL)) {
154 		sc->of_flags |= OFPOLL;
155 		callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
156 	}
157 
158 	return (*tp->t_linesw->l_open)(dev, tp);
159 }
160 
161 int
162 ofcons_close(dev, flag, mode, l)
163 	dev_t dev;
164 	int flag, mode;
165 	struct lwp *l;
166 {
167 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
168 	struct tty *tp = sc->of_tty;
169 
170 	callout_stop(&sc->sc_poll_ch);
171 	sc->of_flags &= ~OFPOLL;
172 	(*tp->t_linesw->l_close)(tp, flag);
173 	ttyclose(tp);
174 	return 0;
175 }
176 
177 int
178 ofcons_read(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_read)(tp, uio, flag);
187 }
188 
189 int
190 ofcons_write(dev, uio, flag)
191 	dev_t dev;
192 	struct uio *uio;
193 	int flag;
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_write)(tp, uio, flag);
199 }
200 
201 int
202 ofcons_poll(dev, events, l)
203 	dev_t dev;
204 	int events;
205 	struct lwp *l;
206 {
207 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
208 	struct tty *tp = sc->of_tty;
209 
210 	return ((*tp->t_linesw->l_poll)(tp, events, l));
211 }
212 int
213 ofcons_ioctl(dev, cmd, data, flag, l)
214 	dev_t dev;
215 	u_long cmd;
216 	caddr_t data;
217 	int flag;
218 	struct lwp *l;
219 {
220 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
221 	struct tty *tp = sc->of_tty;
222 	int error;
223 
224 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
225 		return error;
226 	return ttioctl(tp, cmd, data, flag, l);
227 }
228 
229 struct tty *
230 ofcons_tty(dev)
231 	dev_t dev;
232 {
233 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
234 
235 	return sc->of_tty;
236 }
237 
238 static void
239 ofcons_start(tp)
240 	struct tty *tp;
241 {
242 	struct clist *cl;
243 	int s, len;
244 	u_char buf[OFBURSTLEN];
245 
246 	s = spltty();
247 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
248 		splx(s);
249 		return;
250 	}
251 	tp->t_state |= TS_BUSY;
252 	splx(s);
253 	cl = &tp->t_outq;
254 	len = q_to_b(cl, buf, OFBURSTLEN);
255 	OF_write(stdout, buf, len);
256 	s = spltty();
257 	tp->t_state &= ~TS_BUSY;
258 	if (cl->c_cc) {
259 		tp->t_state |= TS_TIMEOUT;
260 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
261 	}
262 	if (cl->c_cc <= tp->t_lowat) {
263 		if (tp->t_state & TS_ASLEEP) {
264 			tp->t_state &= ~TS_ASLEEP;
265 			wakeup(cl);
266 		}
267 		selwakeup(&tp->t_wsel);
268 	}
269 	splx(s);
270 }
271 
272 static int
273 ofcons_param(tp, t)
274 	struct tty *tp;
275 	struct termios *t;
276 {
277 	tp->t_ispeed = t->c_ispeed;
278 	tp->t_ospeed = t->c_ospeed;
279 	tp->t_cflag = t->c_cflag;
280 	return 0;
281 }
282 
283 static void
284 ofcons_pollin(aux)
285 	void *aux;
286 {
287 	struct ofcons_softc *sc = aux;
288 	struct tty *tp = sc->of_tty;
289 	char ch;
290 
291 	while (OF_read(stdin, &ch, 1) > 0) {
292 		if (tp && (tp->t_state & TS_ISOPEN))
293 			(*tp->t_linesw->l_rint)(ch, tp);
294 	}
295 	callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
296 }
297 
298 static int
299 ofcons_probe()
300 {
301 	int chosen;
302 	char stdinbuf[4], stdoutbuf[4];
303 
304 	if (stdin)
305 		return 1;
306 	if ((chosen = OF_finddevice("/chosen")) == -1)
307 		return 0;
308 	if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) !=
309 	      sizeof stdinbuf ||
310 	    OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) !=
311 	      sizeof stdoutbuf)
312 		return 0;
313 
314 	/* Decode properties. */
315 	stdin = of_decode_int(stdinbuf);
316 	stdout = of_decode_int(stdoutbuf);
317 
318 	return 1;
319 }
320 
321 void
322 ofcons_cnprobe(cd)
323 	struct consdev *cd;
324 {
325 	int maj;
326 
327 	if (!ofcons_probe())
328 		return;
329 
330 	maj = cdevsw_lookup_major(&ofcons_cdevsw);
331 	cd->cn_dev = makedev(maj, 0);
332 	cd->cn_pri = CN_INTERNAL;
333 }
334 
335 void
336 ofcons_cninit(cd)
337 	struct consdev *cd;
338 {
339 }
340 
341 int
342 ofcons_cngetc(dev)
343 	dev_t dev;
344 {
345 	unsigned char ch = '\0';
346 	int l;
347 
348 	while ((l = OF_read(stdin, &ch, 1)) != 1)
349 		if (l != -2 && l != 0)
350 			return -1;
351 	return ch;
352 }
353 
354 void
355 ofcons_cnputc(dev, c)
356 	dev_t dev;
357 	int c;
358 {
359 	char ch = c;
360 
361 	OF_write(stdout, &ch, 1);
362 }
363 
364 void
365 ofcons_cnpollc(dev, on)
366 	dev_t dev;
367 	int on;
368 {
369 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
370 
371 	if (!sc)
372 		return;
373 	if (on) {
374 		if (sc->of_flags & OFPOLL)
375 			callout_stop(&sc->sc_poll_ch);
376 		sc->of_flags &= ~OFPOLL;
377 	} else {
378 		if (!(sc->of_flags & OFPOLL)) {
379 			sc->of_flags |= OFPOLL;
380 			callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
381 		}
382 	}
383 }
384