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